package humanize/*Slightly adapted from the source to fit go-humanize.Author: https://github.com/gorhillSource: https://gist.github.com/gorhill/5285193*/import ()var ( renderFloatPrecisionMultipliers = [...]float64{1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000, } renderFloatPrecisionRounders = [...]float64{0.5,0.05,0.005,0.0005,0.00005,0.000005,0.0000005,0.00000005,0.000000005,0.0000000005, })// FormatFloat produces a formatted number as string based on the following user-specified criteria:// * thousands separator// * decimal separator// * decimal precision//// Usage: s := RenderFloat(format, n)// The format parameter tells how to render the number n.//// See examples: http://play.golang.org/p/LXc1Ddm1lJ//// Examples of format strings, given n = 12345.6789:// "#,###.##" => "12,345.67"// "#,###." => "12,345"// "#,###" => "12345,678"// "#\u202F###,##" => "12 345,68"// "#.###,###### => 12.345,678900// "" (aka default format) => 12,345.67//// The highest precision allowed is 9 digits after the decimal symbol.// There is also a version for integer number, FormatInteger(),// which is convenient for calls within template.func ( string, float64) string {// Special cases: // NaN = "NaN" // +Inf = "+Infinity" // -Inf = "-Infinity"ifmath.IsNaN() {return"NaN" }if > math.MaxFloat64 {return"Infinity" }if < (0.0 - math.MaxFloat64) {return"-Infinity" }// default format := 2 := "." := "," := "" := "-"iflen() > 0 { := []rune()// If there is an explicit format directive, // then default values are these: = 9 = ""// collect indices of meaningful formatting directives := []int{}for , := range {if != '#' && != '0' { = append(, ) } }iflen() > 0 {// Directive at index 0: // Must be a '+' // Raise an error if not the case // index: 0123456789 // +0.000,000 // +000,000.0 // +0000.00 // +0000if [0] == 0 {if [[0]] != '+' {panic("RenderFloat(): invalid positive sign directive") } = "+" = [1:] }// Two directives: // First is thousands separator // Raise an error if not followed by 3-digit // 0123456789 // 0.000,000 // 000,000.00iflen() == 2 {if ([1] - [0]) != 4 {panic("RenderFloat(): thousands separator directive must be followed by 3 digit-specifiers") } = string([[0]]) = [1:] }// One directive: // Directive is decimal separator // The number of digit-specifier following the separator indicates wanted precision // 0123456789 // 0.00 // 000,0000iflen() == 1 { = string([[0]]) = len() - [0] - 1 } } }// generate sign partvarstringif >= 0.000000001 { = } elseif <= -0.000000001 { = = - } else { = "" = 0.0 }// split number into integer and fractional parts , := math.Modf( + renderFloatPrecisionRounders[])// generate integer part string := strconv.FormatInt(int64(), 10)// add thousand separator if requirediflen() > 0 {for := len(); > 3; { -= 3 = [:] + + [:] } }// no fractional part, we can leave nowif == 0 {return + }// generate fractional part := strconv.Itoa(int( * renderFloatPrecisionMultipliers[]))// may need paddingiflen() < { = "000000000000000"[:-len()] + }return + + + }// FormatInteger produces a formatted number as string.// See FormatFloat.func ( string, int) string {returnFormatFloat(, float64())}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.