github.com/tawesoft/golib/v2@v2.10.0/legacy/localize/example_simplenumber_test.go (about) 1 package localize_test 2 3 import ( 4 "fmt" 5 6 "github.com/tawesoft/golib/v2/legacy/localize" 7 "golang.org/x/text/language" 8 "golang.org/x/text/message" 9 ) 10 11 // Demonstrates converting a numbers to and from a strings in a given locale 12 func Example_SimpleNumber() { 13 14 const million = 1_000_000 15 english := language.BritishEnglish 16 dutch := language.Dutch 17 18 message.NewPrinter(english).Printf("A million in English locale is '%d'\n", million) 19 message.NewPrinter(dutch).Printf("But a million in Dutch locale is '%d'\n", million) 20 21 pEng := localize.NewDecimalFormat(english) 22 i, err := pEng.ParseInt("1,234") 23 if err != nil { panic("error parsing a number in English locale:"+err.Error()) } 24 fmt.Printf("1,234 parsed in English locale %T %d\n", i, i) 25 26 pDutch := localize.NewDecimalFormat(dutch) 27 f, err := pDutch.ParseFloat("1,234") 28 if err != nil { panic("error parsing a number in Dutch locale: "+err.Error()) } 29 fmt.Printf("But 1,234 parsed in Dutch locale is %T %f\n", f, f) 30 31 _, err = pDutch.ParseInt("1,234") 32 if err != nil { 33 fmt.Println("In fact, parsing as an integer in the Dutch locale gives an error!") 34 } 35 36 // Output: 37 // A million in English locale is '1,000,000' 38 // But a million in Dutch locale is '1.000.000' 39 // 1,234 parsed in English locale int64 1234 40 // But 1,234 parsed in Dutch locale is float64 1.234000 41 // In fact, parsing as an integer in the Dutch locale gives an error! 42 43 }