duanlang1196 2018-11-29 04:21
浏览 551
已采纳

如何在GO中将float64值修整为特定的小数点? [重复]

This question already has an answer here:

I want to trim latitude and longitude of address upto 5 decimal points. latitude and longitude are of type float64 I have created a function round the value. My function is like this::

func DoubleRoundFive(val float64) float64 {
    formattedVal := fmt.Sprintf("%.5f", val)
    roundedVal, _ := strconv.ParseFloat(formattedVal, 64)
    return roundedVal
}

Output and usage::

DoubleRoundFive(76.70289609999999)

Output::

76.7029

But I just want to trim the value upto 5 decimal points. I want output as 76.70289 . Is it possible in GO?? I want exact 5 decimal values because I am using this for latitude longitude. This is a GO playground Link

</div>
  • 写回答

1条回答 默认 最新

  • doucheng1063 2018-11-29 04:49
    关注

    Try this:

    package main
    
    import (
        "fmt"
    
    )
    
    func main() {
        val := DoubleRoundFive(76.70289609999999)
        fmt.Println(val)
    }
    
    func DoubleRoundFive(val float64) float64 {
        valInt := int64(val*100000)
        val = float64(valInt)/100000
        return val
    }
    

    Go Playground: https://play.golang.org/p/O_H_buvJtDO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?