"1.12345" Instead: ...">

A float in a string with at least one digit after a period

How do I format Float to String:

1 => "1.0"

1.12345 => "1.12345"

Instead:

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

Thank!

+5
source share
3 answers

Is there a maximum limit on the number of digits?

Instead, you can use:

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

You can expand #on what you want / consider reasonable. Following .the format 0specifier , indicates that the decimal place must always be displayed, and #indicates that it should be displayed if present.

+8
source
float f = 1.45783f;
string result = f.ToString("f2");
0
source

toString() IFormatProvider, .

float f = 1.45783f;
string result = f.ToString("f2");
0

All Articles