0

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

Archana Sharma
  • 1,953
  • 6
  • 33
  • 65

1 Answers1

2

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

nightfury1204
  • 4,364
  • 19
  • 22
  • check this for value "40.7564017". It gives only four digit value for this value?? For every type of number I want exact five decimal points whether its 0 or something else. Is it possible? – Archana Sharma Nov 29 '18 at 05:06
  • In float, `40.7564`, `40.75640`, `40.75640000` they all are same. check here: https://play.golang.org/p/COB_J8q1W5S. – nightfury1204 Nov 29 '18 at 05:15