Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format decimal number with set maximal precision but without unneeded trailing zeroes

I Have used following code to format decimals

return string.Format(CultureInfo.CreateSpecificCulture("nb-NO"), "{0:N3}", decVal);

If the decVal do not contain decimals I do not want to show decimal points but I want to show the figure with the correct formatting without zero s , How to perform this ?

like image 681
Gayan Kalanamith Avatar asked Jan 11 '23 07:01

Gayan Kalanamith


1 Answers

You can use custom numeric format like:

return string.Format(CultureInfo.CreateSpecificCulture("nb-NO"), "{0:0.###}", decVal);

You may want to read about standard numeric formats and custom numeric formats

EDIT:

To handle the thousands separators you could use:

return string.Format(CultureInfo.CreateSpecificCulture("nb-NO"), "{0:#,0.###}", decVal);

But, to handle some specific cases you'd better to implement formats like it is described in this SO thread.

P.S.: Thanks @Luaan for the (0.###);

like image 192
Eugene Podskal Avatar answered Jan 29 '23 02:01

Eugene Podskal