microservices-demo/src/currencyservicetemp/money.go
Ahmet Alp Balkan fa46e85220 currencyservicefake: temp replacement for currencysvc
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2018-06-21 22:34:46 -07:00

29 lines
613 B
Go

package main
import (
"fmt"
"math"
"strconv"
"strings"
pb "./genproto"
)
func convert(m pb.MoneyAmount, rate float64) pb.MoneyAmount {
d, f := m.Decimal, m.Fractional
lg := int(math.Max(1, math.Ceil(math.Log10(float64(f)))))
ff, _ := strconv.ParseFloat(fmt.Sprintf("%d.%d", d, f), 64)
res := ff * rate
resTxt := fmt.Sprintf("%."+strconv.Itoa(lg)+"f", res)
p := strings.Split(resTxt, ".")
ds, fs := p[0], p[1]
fs = strings.TrimSuffix(fs, "0")
if fs == "" {
fs = "0"
}
dn, _ := strconv.Atoi(ds)
fn, _ := strconv.Atoi(fs)
return pb.MoneyAmount{Decimal: uint32(dn), Fractional: uint32(fn)}
}