I want to display a float with the entire integer part and up to two decimals for the fractional part, without trailing zeros.
http://play.golang.org/p/mAdQl6erWX:
// Desired output: // "1.9" // "10.9" // "100.9" fmt.Println("2g:") fmt.Println(fmt.Sprintf("%.2g", 1.900)) // outputs "1.9" fmt.Println(fmt.Sprintf("%.2g", 10.900)) // outputs "11" fmt.Println(fmt.Sprintf("%.2g", 100.900)) // outputs "1e+02" fmt.Println("\n2f:") fmt.Println(fmt.Sprintf("%.2f", 1.900)) // outputs "1.90" fmt.Println(fmt.Sprintf("%.2f", 10.900)) // outputs "10.90" fmt.Println(fmt.Sprintf("%.2f", 100.900)) // outputs "100.90"
Formatting with 2g
has the problem that it starts rounding when the integer increases order of magnitudes. Also, it sometimes displays numbers with an e
.
Formatting with 2f
has the problem that it will display trailing zeros. I could write a post-processing function that removes trailing zeros, but I rather do this with Sprintf
.
Can this be done in a generic way using Sprintf
?
If not, what's a good way to do this?
To format floats without trailing zeros with Python, we can use the rstrip method. We interpolate x into a string and then call rstrip with 0 and '. ' to remove trailing zeroes from the number strings. Therefore, n is 3.14.
You can remove trailing zeros using TRIM() function.
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 %.
Trailing zeros (the right most zeros) are significant when there is a decimal point in the number. For this reason it is important to give consideration to when a decimal point is used and to keep the trailing zeros to indicate the actual number of significant figures.
Use the format () function to add zeros to a float after the decimal, e.g. result = format (my_float, '.3f'). The function will format the number with exactly N digits following the decimal point. We used the format function to add zeros to a float after the decimal.
I use this to format floats to trail zeros. You're right. The easiest way is to use " {:g}".format (num) , where: .2f == .00 floats. This is clearly stated not what the question wants.
Below are four functions that can be used to format a number to two decimal places in SQL Server. The most obvious way to do it is to convert the number to a decimal type. Two functions that can do this for us is CAST () and CONVERT ().
x = 211911461911461819112146 y = 2**70 z = x / y print (float (" {:.2f}".format (z))) The wrapping with float () method doesn’t change anything except removing the 0 after the decimal point. The round () is a built-in Python method that returns the floating-point number rounded off to the given digits after the decimal point.
strconv.FormatFloat(10.900, 'f', -1, 64)
This will result in 10.9
.
The -1
as the third parameter tells the function to print the fewest digits necessary to accurately represent the float.
See here: https://golang.org/pkg/strconv/#FormatFloat
Not sure for Sprintf
but to make it worked. Just trim right, first 0
then .
.
fmt.Println(strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", 100.900), "0"), ".")) // 100.9 fmt.Println(strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", 100.0), "0"), ".")) // 100
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