number.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package number
  5. // TODO:
  6. // p.Printf("The gauge was at %v.", number.Spell(number.Percent(23)))
  7. // // Prints: The gauge was at twenty-three percent.
  8. //
  9. // p.Printf("From here to %v!", number.Spell(math.Inf()))
  10. // // Prints: From here to infinity!
  11. //
  12. import (
  13. "golang.org/x/text/internal/number"
  14. )
  15. const (
  16. decimalVerbs = "vfgd"
  17. scientificVerbs = "veg"
  18. )
  19. // Decimal formats a number as a floating point decimal.
  20. func Decimal(x interface{}, opts ...Option) Formatter {
  21. return newFormatter(decimalOptions, opts, x)
  22. }
  23. var decimalOptions = newOptions(decimalVerbs, (*number.Formatter).InitDecimal)
  24. // Scientific formats a number in scientific format.
  25. func Scientific(x interface{}, opts ...Option) Formatter {
  26. return newFormatter(scientificOptions, opts, x)
  27. }
  28. var scientificOptions = newOptions(scientificVerbs, (*number.Formatter).InitScientific)
  29. // Engineering formats a number using engineering notation, which is like
  30. // scientific notation, but with the exponent normalized to multiples of 3.
  31. func Engineering(x interface{}, opts ...Option) Formatter {
  32. return newFormatter(engineeringOptions, opts, x)
  33. }
  34. var engineeringOptions = newOptions(scientificVerbs, (*number.Formatter).InitEngineering)
  35. // Percent formats a number as a percentage. A value of 1.0 means 100%.
  36. func Percent(x interface{}, opts ...Option) Formatter {
  37. return newFormatter(percentOptions, opts, x)
  38. }
  39. var percentOptions = newOptions(decimalVerbs, (*number.Formatter).InitPercent)
  40. // PerMille formats a number as a per mille indication. A value of 1.0 means
  41. // 1000‰.
  42. func PerMille(x interface{}, opts ...Option) Formatter {
  43. return newFormatter(perMilleOptions, opts, x)
  44. }
  45. var perMilleOptions = newOptions(decimalVerbs, (*number.Formatter).InitPerMille)
  46. // TODO:
  47. // - Shortest: akin to verb 'g' of 'G'
  48. //
  49. // TODO: RBNF forms:
  50. // - Compact: 1M 3.5T
  51. // - CompactBinary: 1Mi 3.5Ti
  52. // - Long: 1 million
  53. // - Ordinal:
  54. // - Roman: MCMIIXX
  55. // - RomanSmall: mcmiixx
  56. // - Text: numbers as it typically appears in running text, allowing
  57. // language-specific choices for when to use numbers and when to use words.
  58. // - Spell?: spelled-out number. Maybe just allow as an option?
  59. // NOTE: both spelled-out numbers and ordinals, to render correctly, need
  60. // detailed linguistic information from the translated string into which they
  61. // are substituted. We will need to implement that first.