On Feb 3, 9:15 pm, GGGO <
ggcod...@gmail.com> wrote:
> I can't change my schema's database(my data is DECIMAL(10,4)), so I
> patch it with this :
> tax := total*0.05
> taxstring := fmt.Sprintf("%.2f",tax) // and it round for me here
> tax,_ = strconv.ParseFloat(taxstring,64) // I don't know if it's very
> very dangerous to ignore error returned here, but my data is supposed
> to be always well formated.
>
> I apply this on all float64 value.
> Maybe it could result performance problem but it works for rounding
> and keep decimal to what you want.
> Any feedback on this ?
Feedback - well - it depends on if you're working for a bank or not.
If you are, the advice to use int64 is worth considering. If it's you
own money and you don't mind the rounding errors you will get now and
then I guess your method would be fine. For instance what's the tax
on $0.39 ? In my state it would still be 1 cent, not the rounded 2.
If you're OCD level is up there then you could always use a lookup
table with 100 entries and fill in the exact values for the tax
charged. (Some places with really weird tax rates, like 6.35% this
might be the only reasonable way to know you're getting it right - ie
use the paper tables the state provides to populate your lookup
function and you can stop worrying, plus it's probably a few
nanoseconds faster ;-)
Hotei