Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a float to n decimal places and no trailing zeros

Tags:

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?

like image 747
Blaise Avatar asked Jul 08 '15 10:07

Blaise


People also ask

How do I print float without trailing zeros?

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.

How do you get rid of trailing zeros after a decimal?

You can remove trailing zeros using TRIM() function.

How do you format a float to two 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 %.

Do trailing zeros count if there is a decimal?

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.

How to add zeros to a float after the decimal point?

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.

How do I format floats to trail zeros?

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.

How to format a number to two decimal places in SQL Server?

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 ().

How to round a floating point number to a decimal in Python?

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.


2 Answers

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

like image 122
Eric E. Avatar answered Sep 29 '22 12:09

Eric E.


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 
like image 39
ferhat Avatar answered Sep 29 '22 13:09

ferhat