Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Float as String with at Least 1 Decimal Place

I want to display a float as a string while making sure to display at least one decimal place. If there are more decimals I would like those displayed.

For example: 1 should be displayed as 1.0 1.2345 should display as 1.2345

Can someone help me with the format string?

like image 898
KrisTrip Avatar asked Nov 07 '11 16:11

KrisTrip


People also ask

How do I print just one decimal place in Python?

You should just write "{:. 1f}". format(45.34531) .

How do you show float to 2 decimal places?

format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

Can a float hold a decimal?

If doing math with floats, you need to add a decimal point, otherwise it will be treated as an int. See the Floating point constants page for details. The float data type has only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point.


1 Answers

Use ToString(".0###########") with as much # as decimals you want.

like image 194
Ignacio Soler Garcia Avatar answered Sep 23 '22 01:09

Ignacio Soler Garcia