tkrajina / gpxgo

GPX library for golang
Apache License 2.0
93 stars 23 forks source link

about float format error #21

Closed yangyang5214 closed 8 months ago

yangyang5214 commented 8 months ago
func (f formattedFloat) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
    return xml.Attr{
        Name:  xml.Name{Local: name.Local},
        Value: strings.TrimSuffix(fmt.Sprintf("%.10f", f), "0."),
    }, nil
}

From your git history commit, I know that you are dealing with the case 0.000056

But, when lon="110.0" , An unexpected error will occur

110.0 changed to 11

func TestPoint(t *testing.T) {
    t.Run("test1", func(t *testing.T) {
        f := 110.0
        t.Logf("result is %v", strings.TrimRight(fmt.Sprintf("%.10f", f), "0."))  //  11
        t.Logf("result is %v", strings.TrimSuffix(fmt.Sprintf("%.10f", f), "0.")) // 110.0000000000
    })

    t.Run("test2", func(t *testing.T) {
        f := 0.000056
        t.Logf("result is %v", strings.TrimRight(fmt.Sprintf("%.10f", f), "0."))  //0.000056
        t.Logf("result is %v", strings.TrimSuffix(fmt.Sprintf("%.10f", f), "0.")) //0.000056
    })
}

so, should use strings.TrimSuffix , not strings.TrimRight

yangyang5214 commented 8 months ago

a gpx example

<?xml version="1.0" encoding="UTF-8"?>
<gpx  xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="https://github.com/tkrajina/gpxgo">
    <metadata>
        <author></author>
    </metadata>
     <trk>
        <trkseg>
            <trkpt lat="18.440088272094727" lon="110.0">
                    <ele>9.199999809265137</ele>
                    <time>2024-01-08T02:26:21Z</time>
            </trkpt>
        </trkseg>
    </trk>
</gpx>