Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Format float WITHOUT tail zeros

Tags:

c#

formatting

Is that possible in C# to format

4.52 float number to 4.52 string

and

4.520 float number to 4.52 string, i.e. omitting tail zeros?

EDIT: I think I've not accented the real problem. I need ONE pattern that conforms BOTH of the above examples!

like image 326
Michael Z Avatar asked Jun 21 '26 18:06

Michael Z


1 Answers

Assuming you want to omit any trailing 0's from your value, this should give you what you want:

ToString("0.####")

Otherwise you could do:

ToString("0.00##")
like image 169
James Avatar answered Jun 24 '26 09:06

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!