整数的条件格式化使用小数位数

huangapple go评论85阅读模式
英文:

Conditional formatting of integers using decimal places

问题

我有以下情况:我将收到整数,并根据以下规则进行格式化:

10000 -> 100 // 移除最后的"00"
10010 -> 100.1 // 移除最后的"0",并添加一个小数点
10011 -> 100.11 // 添加两个小数点

如何实现这个功能?非常感谢您的帮助。

英文:

I have the following situation: I'll be receiving integers and have to format them according to the following rules:

10000 -> 100 // removing the last "00"
10010 -> 100.1 // removing the last "0", and adding a decimal place
10011 -> 100.11 // adding two decimal places 

How this can be done? Thanks so much in advance.

答案1

得分: 4

使用浮点数

将整数转换为float64,除以100,并使用fmt包的%g动词,它会去除尾部的零:

> 对于浮点数值,宽度设置字段的最小宽度,精度设置小数点后的位数,如果适用,但对于%g/%G,精度设置最大有效数字的数量(尾部的零会被去除)

为了避免“大”数值转换为%e科学计数法(对于%g,默认精度为6),可以显式指定宽度,例如:

fmt.Printf("%.12g\n", float64(v)/100)

进行测试:

for _, v := range []int{
	10000, 10010, 10011,
	10000000, 10000010, 10000011,
	10000000000, 10000000010, 10000000011,
} {
	fmt.Printf("%.12g\n", float64(v)/100)
}

这将输出(在Go Playground上尝试):

100
100.1
100.11
100000
100000.1
100000.11
100000000
100000000.1
100000000.11

使用整数

如果不将其转换为浮点数(依赖于%g去除尾部零),可以使用整数算术来实现:

最后两位是除以100的余数,其余部分是整数除法的结果。可以根据余数格式化这两个数字,例如:

switch q, r := v/100, v%100; {
case r == 0:
	fmt.Println(q)
case r%10 == 0:
	fmt.Printf("%d.%d\n", q, r/10)
default:
	fmt.Printf("%d.%02d\n", q, r)
}

Go Playground上尝试这个代码。

英文:

Using floating point numbers

Convert the integer number to float64, divide it by 100 and use the %g verb of the fmt package, it removes trailing zeros:

> For floating-point values, width sets the minimum width of the field and precision sets the number of places after the decimal, if appropriate, except that for %g/%G precision sets the maximum number of significant digits (trailing zeros are removed).

To avoid "large" numbers reverting to %e scientific notation (numbers with more than the default precision which is 6 for %g), specify the width explicitly, something like this:

fmt.Printf("%.12g\n", float64(v)/100)

Testing it:

for _, v := range []int{
	10000, 10010, 10011,
	10000000, 10000010, 10000011,
	10000000000, 10000000010, 10000000011,
} {
	fmt.Printf("%.12g\n", float64(v)/100)
}

This will output (try it on the Go Playground):

100
100.1
100.11
100000
100000.1
100000.11
100000000
100000000.1
100000000.11

Using integers

Without converting to floating point numbers (and relying on the trailing zero removal of %g), this is how you could do it using integer arithmetic:

The last 2 digits are the remainder of dividing by 100, the rest is the result of integer division by 100. You can format these 2 numbers depending on the remainder like this:

switch q, r := v/100, v%100; {
case r == 0:
	fmt.Println(q)
case r%10 == 0:
	fmt.Printf("%d.%d\n", q, r/10)
default:
	fmt.Printf("%d.%02d\n", q, r)
}

Try this one on the Go Playground.

huangapple
  • 本文由 发表于 2022年3月15日 22:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/71483820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定