web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Format real number as a string without trailing zeros

(0) ShareShare
ReportReport
Posted on by 213

Hi

How can I best convert a real number such as 16.00000000000000 into a string "16"?

I can use num2str but that doesnt remove the zeros.  And I can loop through the string and remove the zeros and then remove the decimal point as well, but wondered if there was a more elegant solution?

Thanks in advance

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Setting the third argument of num2str() should do the trick.

    You could also convert the real number to int and use int2str().

  • cjohnson300 Profile Picture
    213 on at

    Thanks Martin

    And if the "real" was 16.1250000000000000, would your solutions still hold?  I would like to see 16.125

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    In that case, num2str() is exactly what you need For example: num2Str(16.1250000000000000, -1, -1, -1, -1).

  • cjohnson300 Profile Picture
    213 on at

    What do the -1s indicate?!  It's not in the Microsoft TechNet definition!  And I just tried it and it returned 15.00 for 15.000000000000000

  • Verified answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Oh, sorry, now I see I didn't give you the right answer.

    I would do this:

    str s = System.String::Format("{0:G29}", number);

    It returns 15 and 16.125 in your two cases (I've tested it in D365FO).

    By the way, -1 in num2str() means using defaults.

  • cjohnson300 Profile Picture
    213 on at

    perfect - thank you!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans