web
You’re offline. This is a read only version of the page.
close
Skip to main content
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 203

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)
  • cjohnson300 Profile Picture
    203 on at
    RE: Format real number as a string without trailing zeros

    perfect - thank you!

  • Verified answer
    Martin Dráb Profile Picture
    236,013 Most Valuable Professional on at
    RE: Format real number as a string without trailing zeros

    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
    203 on at
    RE: Format real number as a string without trailing zeros

    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

  • Martin Dráb Profile Picture
    236,013 Most Valuable Professional on at
    RE: Format real number as a string without trailing zeros

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

  • cjohnson300 Profile Picture
    203 on at
    RE: Format real number as a string without trailing zeros

    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
    236,013 Most Valuable Professional on at
    RE: Format real number as a string without trailing zeros

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

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

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Syed Haris Shah Profile Picture

Syed Haris Shah 9

#2
Mea_ Profile Picture

Mea_ 2

#2
Martin Dráb Profile Picture

Martin Dráb 2 Most Valuable Professional

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans