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

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

Conditional formatting of integers using decimal places

问题

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

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

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

英文:

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

  1. 10000 -> 100 // removing the last "00"
  2. 10010 -> 100.1 // removing the last "0", and adding a decimal place
  3. 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),可以显式指定宽度,例如:

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

进行测试:

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

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

  1. 100
  2. 100.1
  3. 100.11
  4. 100000
  5. 100000.1
  6. 100000.11
  7. 100000000
  8. 100000000.1
  9. 100000000.11

使用整数

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

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

  1. switch q, r := v/100, v%100; {
  2. case r == 0:
  3. fmt.Println(q)
  4. case r%10 == 0:
  5. fmt.Printf("%d.%d\n", q, r/10)
  6. default:
  7. fmt.Printf("%d.%02d\n", q, r)
  8. }

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:

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

Testing it:

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

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

  1. 100
  2. 100.1
  3. 100.11
  4. 100000
  5. 100000.1
  6. 100000.11
  7. 100000000
  8. 100000000.1
  9. 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:

  1. switch q, r := v/100, v%100; {
  2. case r == 0:
  3. fmt.Println(q)
  4. case r%10 == 0:
  5. fmt.Printf("%d.%d\n", q, r/10)
  6. default:
  7. fmt.Printf("%d.%02d\n", q, r)
  8. }

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:

确定