Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float to string with at least one digit after period

Tags:

c#

How do I format a Float to String:

1 => "1.0"

1.12345 => "1.12345"

Instead of:

 String.Format("{0:0.0}", 123.0); // Limit amount of digits

Thank you!

like image 899
Gerhard Powell Avatar asked May 10 '12 16:05

Gerhard Powell


1 Answers

Is there a maximum limit to the number of digits?

You can instead use:

String.Format("{0:0.0#####}", floatVal)

You can extend the # out to whatever you want/consider reasonable. Following the . of the format specifier, a 0 indicates the decimal precision place should always be shown, while # indicates it should be shown if present.

like image 58
Mike Guthrie Avatar answered Oct 21 '22 23:10

Mike Guthrie