github.com/tawesoft/golib/v2@v2.10.0/legacy/localize/example_manynumbers_test.go (about)

     1  package localize_test
     2  
     3  import (
     4      "fmt"
     5  
     6      "github.com/tawesoft/golib/v2/legacy/localize"
     7      "github.com/tawesoft/golib/v2/must"
     8      "golang.org/x/text/language"
     9      "golang.org/x/text/language/display"
    10      "golang.org/x/text/message"
    11      "golang.org/x/text/number"
    12  )
    13  
    14  // Demonstrates converting a numbers to and from a strings in a given locale,
    15  // with example outputs for a variety of locales.
    16  func Example_ManyNumbers() {
    17      const input = 123456789.012
    18  
    19      langs := []language.Tag{
    20          language.Arabic,
    21          language.BritishEnglish,
    22          language.Dutch,
    23          language.French,
    24          language.Malayalam,
    25          language.Bengali,
    26      }
    27  
    28      // language name as a string
    29      namer := display.Tags(language.English)
    30  
    31      for _, t := range langs {
    32          printer := message.NewPrinter(t)
    33          localized := printer.Sprintf("%.4f", number.Decimal(input))
    34  
    35          parser := localize.NewDecimalFormat(t)
    36          result := must.Result(parser.ParseFloat(localized))
    37  
    38          fmt.Printf("Language: %s\nPrints %T %.4f as %q\nParses %q back to %T %.4f\n\n",
    39              namer.Name(t), input, input, localized, localized, result, result)
    40      }
    41  
    42      // Output:
    43      // Language: Arabic
    44      // Prints float64 123456789.0120 as "١٢٣٬٤٥٦٬٧٨٩٫٠١٢٠"
    45      // Parses "١٢٣٬٤٥٦٬٧٨٩٫٠١٢٠" back to float64 123456789.0120
    46      //
    47      // Language: British English
    48      // Prints float64 123456789.0120 as "123,456,789.0120"
    49      // Parses "123,456,789.0120" back to float64 123456789.0120
    50      //
    51      // Language: Dutch
    52      // Prints float64 123456789.0120 as "123.456.789,0120"
    53      // Parses "123.456.789,0120" back to float64 123456789.0120
    54      //
    55      // Language: French
    56      // Prints float64 123456789.0120 as "123\u00a0456\u00a0789,0120"
    57      // Parses "123\u00a0456\u00a0789,0120" back to float64 123456789.0120
    58      //
    59      // Language: Malayalam
    60      // Prints float64 123456789.0120 as "12,34,56,789.0120"
    61      // Parses "12,34,56,789.0120" back to float64 123456789.0120
    62      //
    63      // Language: Bangla
    64      // Prints float64 123456789.0120 as "১২,৩৪,৫৬,৭৮৯.০১২০"
    65      // Parses "১২,৩৪,৫৬,৭৮৯.০১২০" back to float64 123456789.0120
    66  }