| 1 |
package strconv |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
) |
| 6 |
|
| 7 |
// ParseDecimal parses number of the format 1.2 |
| 8 |
func ParseDecimal(b []byte) (float64, int) { |
| 9 |
// float64 has up to 17 significant decimal digits and an exponent in [-1022,1023] |
| 10 |
i := 0 |
| 11 |
sign := 1.0 |
| 12 |
if 0 < len(b) && b[0] == '-' { |
| 13 |
sign = -1.0 |
| 14 |
i++ |
| 15 |
} |
| 16 |
|
| 17 |
start := -1 |
| 18 |
dot := -1 |
| 19 |
n := uint64(0) |
| 20 |
for ; i < len(b); i++ { |
| 21 |
// parse up to 18 significant digits (with dot will be 17) ignoring zeros before/after |
| 22 |
c := b[i] |
| 23 |
if '0' <= c && c <= '9' { |
| 24 |
if start == -1 { |
| 25 |
if '1' <= c && c <= '9' { |
| 26 |
n = uint64(c - '0') |
| 27 |
start = i |
| 28 |
} |
| 29 |
} else if i-start < 18 { |
| 30 |
n *= 10 |
| 31 |
n += uint64(c - '0') |
| 32 |
} |
| 33 |
} else if c == '.' { |
| 34 |
if dot != -1 { |
| 35 |
break |
| 36 |
} |
| 37 |
dot = i |
| 38 |
} else { |
| 39 |
break |
| 40 |
} |
| 41 |
} |
| 42 |
if i == 1 && dot == 0 { |
| 43 |
return 0.0, 0 // only dot |
| 44 |
} else if start == -1 { |
| 45 |
return 0.0, i // only zeros and dot |
| 46 |
} else if dot == -1 { |
| 47 |
dot = i |
| 48 |
} |
| 49 |
|
| 50 |
exp := (dot - start) - LenUint(n) |
| 51 |
if dot < start { |
| 52 |
exp++ |
| 53 |
} |
| 54 |
if 1023 < exp { |
| 55 |
if sign == 1.0 { |
| 56 |
return math.Inf(1), i |
| 57 |
} else { |
| 58 |
return math.Inf(-1), i |
| 59 |
} |
| 60 |
} else if exp < -1022 { |
| 61 |
return 0.0, i |
| 62 |
} |
| 63 |
|
| 64 |
f := sign * float64(n) |
| 65 |
if 0 <= exp && exp < 23 { |
| 66 |
return f * float64pow10[exp], i |
| 67 |
} else if -22 <= exp && exp < 0 { // int / 10^k |
| 68 |
return f / float64pow10[-exp], i |
| 69 |
} |
| 70 |
return f * math.Pow10(exp), i |
| 71 |
} |
| 72 |
|
| 73 |
// AppendDecimal appends a float to `b` with `dec` the maximum number of decimals. |
| 74 |
func AppendDecimal(b []byte, f float64, dec int) []byte { |
| 75 |
if math.IsNaN(float64(f)) || math.IsInf(float64(f), 0) { |
| 76 |
return b |
| 77 |
} |
| 78 |
|
| 79 |
if dec < 0 || 17 < dec { |
| 80 |
dec = 17 |
| 81 |
} |
| 82 |
f *= math.Pow10(dec) |
| 83 |
|
| 84 |
// correct rounding |
| 85 |
if 0.0 <= f { |
| 86 |
f += 0.5 |
| 87 |
} else { |
| 88 |
f -= 0.5 |
| 89 |
} |
| 90 |
|
| 91 |
// calculate mantissa and exponent |
| 92 |
num := int64(f) |
| 93 |
if num == 0 { |
| 94 |
return append(b, '0') |
| 95 |
} |
| 96 |
for 0 < dec && num%10 == 0 { |
| 97 |
num /= 10 |
| 98 |
dec-- // remove trailing zeros |
| 99 |
} |
| 100 |
|
| 101 |
i, n := len(b), LenInt(num) |
| 102 |
if 0 < dec { |
| 103 |
if n < dec { |
| 104 |
n = dec // number has zero after dot |
| 105 |
} |
| 106 |
n++ // dot |
| 107 |
if lim := int64pow10[dec]; 0 < num && num < lim || num < 0 && -lim < num { |
| 108 |
n++ // zero at beginning |
| 109 |
} |
| 110 |
} |
| 111 |
if cap(b) < i+n { |
| 112 |
b = append(b, make([]byte, n)...) |
| 113 |
} else { |
| 114 |
b = b[:i+n] |
| 115 |
} |
| 116 |
|
| 117 |
// print sign |
| 118 |
if num < 0 { |
| 119 |
num = -num |
| 120 |
b[i] = '-' |
| 121 |
} |
| 122 |
i += n - 1 |
| 123 |
|
| 124 |
// print number |
| 125 |
if 0 < dec { |
| 126 |
b[i] = byte(num%10) + '0' |
| 127 |
num /= 10 |
| 128 |
dec-- |
| 129 |
i-- |
| 130 |
for 0 < dec { |
| 131 |
b[i] = byte(num%10) + '0' |
| 132 |
num /= 10 |
| 133 |
dec-- |
| 134 |
i-- |
| 135 |
} |
| 136 |
b[i] = '.' |
| 137 |
i-- |
| 138 |
} |
| 139 |
if num == 0 { |
| 140 |
b[i] = '0' |
| 141 |
} else { |
| 142 |
for num != 0 { |
| 143 |
b[i] = byte(num%10) + '0' |
| 144 |
num /= 10 |
| 145 |
i-- |
| 146 |
} |
| 147 |
} |
| 148 |
return b |
| 149 |
} |