Original post

I have a field for the amounts and the type of the field is float64, so is it possible if I can trim the leading 0’s?

For example: if Amount = 0.00 then delate the leading 0s so the amount only shows as 0 but if the Amount has cents then show two decimals.

package main

import (

func main() {
        var test float64 = 0.0
        test = 0.1

func Float2String(xF float64) string {
        if math.Trunc(xF) == xF {
                return fmt.Sprintf("%.0f", xF)
        } else {
                return fmt.Sprintf("%.2f", xF)

I was reading the documentation and I didn’t found another option.

Cents as in currency? Please stop right here and search for a proper money library! (Or fixed decimals at least)

Floating point numbers are not the right tool for anything that needs to be exact, and money needs to be exact!

Always remember that with floating point not even the most trivial mathematical laws hold… (x + y) + z is not necessarily the same as x + (y + z)!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.