douyin2962
2014-07-20 16:03
浏览 105
已采纳

如何计算浮点数的小数位?

I want to check if a float32 has two decimal places or not. My javascript way to do this would look like:

step  := 0.01
value := 9.99

if int(value/step) % 1 == 0 {
    printf("has two decimal places!")
}

The above example also works. However it will not work when step is incorrect as go then cannot properly cast from float64 to int.

Example:

step  := 0.1
value := 9.99

if int(value/step) % 1 == 0 {
    printf("has two decimal places!")
}

Compiler Error: constant 9.99 truncated to integer

When we use dynamic values it will just return true for every case.

So what is the appropriate way to count decimal places?

图片转代码服务由CSDN问答提供 功能建议

我想检查float32是否有两位小数。 我的javascript方法如下:

  step:= 0.01 
value:= 9.99 
 
if int(value / step)%1 == 0 {\  n printf(“具有两个小数位!”)
} 
   
 
 

上面的示例也适用。 但是,当step不正确时,它将无法正常工作,因为go不能正确地将其从float64强制转换为int。

示例:

  step:=  0.1 
value:= 9.99 
 
if int(值/步)%1 == 0 {
 printf(“有两个小数位!”)
} 
   
 \  n 

编译器错误:常量9.99被截断为整数

当我们使用动态值时,它只会在每种情况下返回true。

那么计算小数位的适当方法是什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dreinuqm992401 2014-07-20 16:16
    已采纳

    int value % 1 is always zero!

    I suggest an alternative way:

    value := float32(9.99)
    valuef := value*100
    extra := valuef - float32(int(valuef))
    if extra < 1e-5 {
        fmt.Println("has two decimal places!");
    }
    

    http://play.golang.org/p/LQQ8T6SIY2

    Update

    package main
    
    import (
        "math"
    )
    
    func main() {
        value := float32(9.9990001)
    
        println(checkDecimalPlaces(3, value))
    }
    
    func checkDecimalPlaces(i int, value float32) bool {
        valuef := value * float32(math.Pow(10.0, float64(i)))
        println(valuef)
        extra := valuef - float32(int(valuef))
    
        return extra == 0
    }
    

    http://play.golang.org/p/jXRhHsCYL-

    点赞 打赏 评论
  • dongyidao1461 2014-07-20 16:08

    You have to trick it, add an extra variable:

    step := 0.1
    value := 9.99
    steps := value / step
    if int(steps)%1 == 0 {
        fmt.Println("has two decimal places!")
    }
    

    Or cast your steps before you convert it to int like:

    int(float64(value / step))
    

    playground

    //edit

    the hacky non-mathematical way is to convert it to a string and split it, example:

    func NumDecPlaces(v float64) int {
        s := strconv.FormatFloat(v, 'f', -1, 64)
        i := strings.IndexByte(s, '.')
        if i > -1 {
            return len(s) - i - 1
        }
        return 0
    }
    

    playground

    //updated with a minor optimization

    点赞 打赏 评论
  • doubi1928 2019-04-20 00:38

    Here's a function to get the decimal portion of a float. Can use len(decimalPortion(n)) to get the number of decimal places.

    func decimalPortion(n float64) string {
        decimalPlaces := fmt.Sprintf("%f", n-math.Floor(n)) // produces 0.xxxx0000
        decimalPlaces = strings.Replace(decimalPlaces, "0.", "", -1) // remove 0.
        decimalPlaces = strings.TrimRight(decimalPlaces, "0") // remove trailing 0s
        return decimalPlaces
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题