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?
You should just write "{:. 1f}". format(45.34531) .
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 %.
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.
Use ToString(".0###########") with as much # as decimals you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With