dongtiannan0367 2013-10-08 02:37
浏览 114
已采纳

在Go中将货币从float转换为integer的最佳方法是什么?

What is the best way to convert a currency from float to integer in Go?

------------- Added Explanation of question ----------

To expand my question a little, the following is an example of what I see as the “problem” solved by adding a rounding value of 0.004 (for 2-decimal currencies).

As far as I know, external values stored as eg. decimal, double, currency in an RDBMS need to be “imported” to Go as a float. In order to perform calculations, they then need to be converted to integer, or at least that is one method.

In the example below, fVal1 emulates an amount imported from a database. To perform calculations on it, I want to convert it to an integer. The easiest way to do that appears to me to be to add a rounding figure as illustrated.

Example code:

var fVal1 float64 = 19.08
var f100 float64 = 100.0
var fRound float64 = 0.004
var fResult float64 = fVal1 * f100
fmt.Printf("%f * %f as float = %f
", fVal1, f100, fResult)
var iResult1 int64 = int64(fResult)
fmt.Printf("as Integer unrounded = %d
", iResult1)
var iResult2 int64 = int64(fResult + fRound)
fmt.Printf("as Integer rounded = %d
", iResult2)

Console output:

19.080000 * 100.000000 as float = 1908.000000
as Integer unrounded = 1907
as Integer rounded = 1908

----------- end of addition to question -----------

I’ve implemented a small package to handle multiple currencies in Go, and essentially it revolves around the following bit of code (below).

When I discovered that there were rounding errors in converting between floats and integers, the first rounding factor that I tried was 0.004 (for 2 decimals), and it appeared to work OK.

Basically, the following bit of code for currency conversion from float to int revolves around these two alternative lines of code :

  var iCcyAmt1 int64 = int64(fCcyBase * fScale)
  var iCcyAmt2 int64 = int64((fCcyBase + fRound) * fScale)

The rounding being used in “fRound” in the second alternative is “0.004”.

Running this test program (10 million iterations) results in a consistent rounding error of about 588,000 cents where the “0.004” is not added, and zero difference using the rounding figure.

Is this a safe way to handle this situation (I don’t want to use strings), or is there a better way?

Test Program:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    var (
        fRound      float64 = 0.004
        fScale      float64 = 100.0
        iCcyTotBase int64
        iCcyTot1    int64
        iCcyTot2    int64
    )

    const I_ITERS int = 10000000

    rand.Seed(time.Now().UTC().UnixNano())

    fmt.Printf("
Testing Float to Integer (%d iterations)"+
        " .........", I_ITERS)

    for i := 0; i < I_ITERS; i++ {
        var iCcyBase int64 = int64(999 + rand.Intn(9999999))
        var fCcyBase float64 = float64(iCcyBase) / fScale
        var iCcyAmt1 int64 = int64(fCcyBase * fScale)
        var iCcyAmt2 int64 = int64((fCcyBase + fRound) * fScale)
        iCcyTotBase += iCcyBase
        iCcyTot1 += iCcyAmt1
        iCcyTot2 += iCcyAmt2
    }

    var iCcyDiff1 int64 = iCcyTot1 - iCcyTotBase
    var iCcyDiff2 int64 = iCcyTot2 - iCcyTotBase
    fmt.Printf("
Diff without rounding = %d
", iCcyDiff1)
    fmt.Printf("Diff with rounding = %d
", iCcyDiff2)
}
  • 写回答

3条回答 默认 最新

  • doufangmu9087 2013-10-08 17:34
    关注

    You have to round (as you've done). And -as you've alluded to- the nature of floating points make this difficult, and you have to deal with some bad decimal results due to rounding errors. AFAIK, golang doesn't have a rounding function, so you'll need to implement it. I've seen this gist tweeted/mentioned a few times... and it seems to have been born in this thread.

    func RoundViaFloat(x float64, prec int) float64 {
        var rounder float64
        pow := math.Pow(10, float64(prec))
        intermed := x * pow
        _, frac := math.Modf(intermed)
        intermed += .5
        x = .5
        if frac < 0.0 {
            x=-.5
            intermed -=1
        }
        if frac >= x {
            rounder = math.Ceil(intermed)
        } else {
            rounder = math.Floor(intermed)
        }
    
        return rounder / pow
    }
    

    A couple of notes/resources (for others that show up later):

    • When converting a floating point via int64() the fractional part will be discarded.

    • When printing a floating point use the correct golang format. e.g. fmt.Printf("%.20f * %.20f as float = %.20f ", fVal1, f100, fResult)

    • Use an Integer for money. Avoid all of this by using pennies (and integer math in order to avoid the rounding) and divide by 100. i.e. use a large enough fixed size integer or math/big.Int and avoid the floating point arithmetic.

    Note: You can, alternatively, round using strconv (the OP wanted to avoid this):

    func RoundFloat(x float64, prec int) float64 {
        frep := strconv.FormatFloat(x, 'g', prec, 64)
        f, _ := strconv.ParseFloat(frep, 64)
        return f
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀