blob: b58001888e8002fb4bad8380d99abe43e37ed02c [file] [log] [blame]
Russ Cox079c00a2008-11-17 12:34:03 -08001// Copyright 2009 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
5// Multiprecision decimal numbers.
6// For floating-point formatting only; not general purpose.
7// Only operations are assign and (binary) left/right shift.
8// Can do binary floating point in multiprecision decimal precisely
9// because 2 divides 10; cannot do decimal floating point
10// in multiprecision binary precisely.
11
12package strconv
13
Russ Cox8a7cbad2009-01-15 17:22:17 -080014type decimal struct {
Robert Griesemer9209d892015-02-02 16:42:40 -080015 d [800]byte // digits, big-endian representation
Russ Cox3ee20852012-02-07 23:37:15 -050016 nd int // number of digits used
17 dp int // decimal point
Adam Kisala8cb40fa2017-07-15 19:27:56 +010018 neg bool // negative flag
19 trunc bool // discarded nonzero digits beyond d[:nd]
Robert Griesemer314b3582009-11-04 23:20:49 -080020}
Russ Cox079c00a2008-11-17 12:34:03 -080021
Russ Cox8a7cbad2009-01-15 17:22:17 -080022func (a *decimal) String() string {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080023 n := 10 + a.nd
Russ Cox079c00a2008-11-17 12:34:03 -080024 if a.dp > 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080025 n += a.dp
Russ Cox079c00a2008-11-17 12:34:03 -080026 }
27 if a.dp < 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080028 n += -a.dp
Russ Cox079c00a2008-11-17 12:34:03 -080029 }
30
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080031 buf := make([]byte, n)
32 w := 0
Russ Cox079c00a2008-11-17 12:34:03 -080033 switch {
Russ Coxcf9b7f72008-11-19 12:50:34 -080034 case a.nd == 0:
Robert Griesemer40621d52009-11-09 12:07:39 -080035 return "0"
Russ Coxcf9b7f72008-11-19 12:50:34 -080036
Russ Cox079c00a2008-11-17 12:34:03 -080037 case a.dp <= 0:
38 // zeros fill space between decimal point and digits
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080039 buf[w] = '0'
40 w++
41 buf[w] = '.'
42 w++
43 w += digitZero(buf[w : w+-a.dp])
Russ Cox7d7ebd22010-05-03 17:47:40 -070044 w += copy(buf[w:], a.d[0:a.nd])
Russ Cox079c00a2008-11-17 12:34:03 -080045
46 case a.dp < a.nd:
47 // decimal point in middle of digits
Russ Cox7d7ebd22010-05-03 17:47:40 -070048 w += copy(buf[w:], a.d[0:a.dp])
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080049 buf[w] = '.'
50 w++
Russ Cox7d7ebd22010-05-03 17:47:40 -070051 w += copy(buf[w:], a.d[a.dp:a.nd])
Russ Cox079c00a2008-11-17 12:34:03 -080052
53 default:
54 // zeros fill space between digits and decimal point
Russ Cox7d7ebd22010-05-03 17:47:40 -070055 w += copy(buf[w:], a.d[0:a.nd])
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080056 w += digitZero(buf[w : w+a.dp-a.nd])
Russ Cox079c00a2008-11-17 12:34:03 -080057 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080058 return string(buf[0:w])
Russ Cox079c00a2008-11-17 12:34:03 -080059}
60
Russ Cox8a7cbad2009-01-15 17:22:17 -080061func digitZero(dst []byte) int {
Russ Cox7d7ebd22010-05-03 17:47:40 -070062 for i := range dst {
Robert Griesemer40621d52009-11-09 12:07:39 -080063 dst[i] = '0'
Russ Cox079c00a2008-11-17 12:34:03 -080064 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080065 return len(dst)
Russ Cox079c00a2008-11-17 12:34:03 -080066}
67
Russ Cox8a7cbad2009-01-15 17:22:17 -080068// trim trailing zeros from number.
Russ Cox079c00a2008-11-17 12:34:03 -080069// (They are meaningless; the decimal point is tracked
70// independent of the number of digits.)
Russ Cox8a7cbad2009-01-15 17:22:17 -080071func trim(a *decimal) {
Robert Griesemer3bb00322009-11-09 21:23:52 -080072 for a.nd > 0 && a.d[a.nd-1] == '0' {
Robert Griesemer40621d52009-11-09 12:07:39 -080073 a.nd--
Russ Cox079c00a2008-11-17 12:34:03 -080074 }
75 if a.nd == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080076 a.dp = 0
Russ Cox079c00a2008-11-17 12:34:03 -080077 }
78}
79
80// Assign v to a.
Russ Cox8a7cbad2009-01-15 17:22:17 -080081func (a *decimal) Assign(v uint64) {
Rémy Oudomphengd6147d82012-07-10 07:44:23 +020082 var buf [24]byte
Russ Cox079c00a2008-11-17 12:34:03 -080083
84 // Write reversed decimal in buf.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080085 n := 0
Russ Cox079c00a2008-11-17 12:34:03 -080086 for v > 0 {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080087 v1 := v / 10
88 v -= 10 * v1
89 buf[n] = byte(v + '0')
90 n++
91 v = v1
Russ Cox079c00a2008-11-17 12:34:03 -080092 }
93
94 // Reverse again to produce forward decimal in a.d.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080095 a.nd = 0
Robert Griesemer314b3582009-11-04 23:20:49 -080096 for n--; n >= 0; n-- {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080097 a.d[a.nd] = buf[n]
98 a.nd++
Russ Cox079c00a2008-11-17 12:34:03 -080099 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800100 a.dp = a.nd
101 trim(a)
Russ Cox079c00a2008-11-17 12:34:03 -0800102}
103
Russ Cox079c00a2008-11-17 12:34:03 -0800104// Maximum shift that we can do in one pass without overflow.
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800105// A uint has 32 or 64 bits, and we have to be able to accommodate 9<<k.
106const uintSize = 32 << (^uint(0) >> 63)
107const maxShift = uintSize - 4
Russ Cox079c00a2008-11-17 12:34:03 -0800108
Robert Griesemer9209d892015-02-02 16:42:40 -0800109// Binary shift right (/ 2) by k bits. k <= maxShift to avoid overflow.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800110func rightShift(a *decimal, k uint) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800111 r := 0 // read pointer
112 w := 0 // write pointer
Russ Cox079c00a2008-11-17 12:34:03 -0800113
114 // Pick up enough leading digits to cover first shift.
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800115 var n uint
Russ Cox079c00a2008-11-17 12:34:03 -0800116 for ; n>>k == 0; r++ {
117 if r >= a.nd {
118 if n == 0 {
Russ Coxcf9b7f72008-11-19 12:50:34 -0800119 // a == 0; shouldn't get here, but handle anyway.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800120 a.nd = 0
121 return
Russ Cox079c00a2008-11-17 12:34:03 -0800122 }
Russ Coxcf9b7f72008-11-19 12:50:34 -0800123 for n>>k == 0 {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800124 n = n * 10
125 r++
Russ Cox079c00a2008-11-17 12:34:03 -0800126 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800127 break
Russ Cox079c00a2008-11-17 12:34:03 -0800128 }
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800129 c := uint(a.d[r])
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800130 n = n*10 + c - '0'
Russ Cox079c00a2008-11-17 12:34:03 -0800131 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800132 a.dp -= r - 1
Russ Cox079c00a2008-11-17 12:34:03 -0800133
Alberto Donizetticbf28ff2016-10-01 14:01:09 +0200134 var mask uint = (1 << k) - 1
135
Russ Cox079c00a2008-11-17 12:34:03 -0800136 // Pick up a digit, put down a digit.
137 for ; r < a.nd; r++ {
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800138 c := uint(a.d[r])
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800139 dig := n >> k
Alberto Donizetticbf28ff2016-10-01 14:01:09 +0200140 n &= mask
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800141 a.d[w] = byte(dig + '0')
142 w++
143 n = n*10 + c - '0'
Russ Cox079c00a2008-11-17 12:34:03 -0800144 }
145
146 // Put down extra digits.
147 for n > 0 {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800148 dig := n >> k
Alberto Donizetticbf28ff2016-10-01 14:01:09 +0200149 n &= mask
Russ Cox3ee20852012-02-07 23:37:15 -0500150 if w < len(a.d) {
151 a.d[w] = byte(dig + '0')
152 w++
153 } else if dig > 0 {
154 a.trunc = true
155 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800156 n = n * 10
Russ Cox079c00a2008-11-17 12:34:03 -0800157 }
158
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800159 a.nd = w
160 trim(a)
Russ Cox079c00a2008-11-17 12:34:03 -0800161}
162
163// Cheat sheet for left shift: table indexed by shift count giving
164// number of new digits that will be introduced by that shift.
165//
Russ Cox8a7cbad2009-01-15 17:22:17 -0800166// For example, leftcheats[4] = {2, "625"}. That means that
Russ Cox079c00a2008-11-17 12:34:03 -0800167// if we are shifting by 4 (multiplying by 16), it will add 2 digits
168// when the string prefix is "625" through "999", and one fewer digit
169// if the string prefix is "000" through "624".
170//
171// Credit for this trick goes to Ken.
172
Russ Cox8a7cbad2009-01-15 17:22:17 -0800173type leftCheat struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800174 delta int // number of new digits
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800175 cutoff string // minus one digit if original < a.
Russ Cox079c00a2008-11-17 12:34:03 -0800176}
177
Robert Griesemer314b3582009-11-04 23:20:49 -0800178var leftcheats = []leftCheat{
Russ Cox079c00a2008-11-17 12:34:03 -0800179 // Leading digits of 1/2^i = 5^i.
180 // 5^23 is not an exact 64-bit floating point number,
181 // so have to use bc for the math.
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800182 // Go up to 60 to be large enough for 32bit and 64bit platforms.
Russ Cox079c00a2008-11-17 12:34:03 -0800183 /*
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800184 seq 60 | sed 's/^/5^/' | bc |
185 awk 'BEGIN{ print "\t{ 0, \"\" }," }
Robert Griesemer314b3582009-11-04 23:20:49 -0800186 {
187 log2 = log(2)/log(10)
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800188 printf("\t{ %d, \"%s\" },\t// * %d\n",
Robert Griesemer314b3582009-11-04 23:20:49 -0800189 int(log2*NR+1), $0, 2**NR)
190 }'
191 */
Robert Griesemer34788912010-10-22 10:06:33 -0700192 {0, ""},
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800193 {1, "5"}, // * 2
194 {1, "25"}, // * 4
195 {1, "125"}, // * 8
196 {2, "625"}, // * 16
197 {2, "3125"}, // * 32
198 {2, "15625"}, // * 64
199 {3, "78125"}, // * 128
200 {3, "390625"}, // * 256
201 {3, "1953125"}, // * 512
202 {4, "9765625"}, // * 1024
203 {4, "48828125"}, // * 2048
204 {4, "244140625"}, // * 4096
205 {4, "1220703125"}, // * 8192
206 {5, "6103515625"}, // * 16384
207 {5, "30517578125"}, // * 32768
208 {5, "152587890625"}, // * 65536
209 {6, "762939453125"}, // * 131072
210 {6, "3814697265625"}, // * 262144
211 {6, "19073486328125"}, // * 524288
212 {7, "95367431640625"}, // * 1048576
213 {7, "476837158203125"}, // * 2097152
214 {7, "2384185791015625"}, // * 4194304
215 {7, "11920928955078125"}, // * 8388608
216 {8, "59604644775390625"}, // * 16777216
217 {8, "298023223876953125"}, // * 33554432
218 {8, "1490116119384765625"}, // * 67108864
219 {9, "7450580596923828125"}, // * 134217728
220 {9, "37252902984619140625"}, // * 268435456
221 {9, "186264514923095703125"}, // * 536870912
222 {10, "931322574615478515625"}, // * 1073741824
223 {10, "4656612873077392578125"}, // * 2147483648
224 {10, "23283064365386962890625"}, // * 4294967296
225 {10, "116415321826934814453125"}, // * 8589934592
226 {11, "582076609134674072265625"}, // * 17179869184
227 {11, "2910383045673370361328125"}, // * 34359738368
228 {11, "14551915228366851806640625"}, // * 68719476736
229 {12, "72759576141834259033203125"}, // * 137438953472
230 {12, "363797880709171295166015625"}, // * 274877906944
231 {12, "1818989403545856475830078125"}, // * 549755813888
232 {13, "9094947017729282379150390625"}, // * 1099511627776
233 {13, "45474735088646411895751953125"}, // * 2199023255552
234 {13, "227373675443232059478759765625"}, // * 4398046511104
235 {13, "1136868377216160297393798828125"}, // * 8796093022208
236 {14, "5684341886080801486968994140625"}, // * 17592186044416
237 {14, "28421709430404007434844970703125"}, // * 35184372088832
238 {14, "142108547152020037174224853515625"}, // * 70368744177664
239 {15, "710542735760100185871124267578125"}, // * 140737488355328
240 {15, "3552713678800500929355621337890625"}, // * 281474976710656
241 {15, "17763568394002504646778106689453125"}, // * 562949953421312
242 {16, "88817841970012523233890533447265625"}, // * 1125899906842624
243 {16, "444089209850062616169452667236328125"}, // * 2251799813685248
244 {16, "2220446049250313080847263336181640625"}, // * 4503599627370496
245 {16, "11102230246251565404236316680908203125"}, // * 9007199254740992
246 {17, "55511151231257827021181583404541015625"}, // * 18014398509481984
247 {17, "277555756156289135105907917022705078125"}, // * 36028797018963968
248 {17, "1387778780781445675529539585113525390625"}, // * 72057594037927936
249 {18, "6938893903907228377647697925567626953125"}, // * 144115188075855872
250 {18, "34694469519536141888238489627838134765625"}, // * 288230376151711744
251 {18, "173472347597680709441192448139190673828125"}, // * 576460752303423488
252 {19, "867361737988403547205962240695953369140625"}, // * 1152921504606846976
Russ Coxbe2edb52009-03-03 08:39:12 -0800253}
Russ Cox079c00a2008-11-17 12:34:03 -0800254
255// Is the leading prefix of b lexicographically less than s?
Russ Cox8a7cbad2009-01-15 17:22:17 -0800256func prefixIsLessThan(b []byte, s string) bool {
Russ Cox079c00a2008-11-17 12:34:03 -0800257 for i := 0; i < len(s); i++ {
258 if i >= len(b) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800259 return true
Russ Cox079c00a2008-11-17 12:34:03 -0800260 }
261 if b[i] != s[i] {
Robert Griesemer40621d52009-11-09 12:07:39 -0800262 return b[i] < s[i]
Russ Cox079c00a2008-11-17 12:34:03 -0800263 }
264 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800265 return false
Russ Cox079c00a2008-11-17 12:34:03 -0800266}
267
Robert Griesemer9209d892015-02-02 16:42:40 -0800268// Binary shift left (* 2) by k bits. k <= maxShift to avoid overflow.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800269func leftShift(a *decimal, k uint) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800270 delta := leftcheats[k].delta
Robert Griesemer3bb00322009-11-09 21:23:52 -0800271 if prefixIsLessThan(a.d[0:a.nd], leftcheats[k].cutoff) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800272 delta--
Russ Cox079c00a2008-11-17 12:34:03 -0800273 }
274
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800275 r := a.nd // read index
276 w := a.nd + delta // write index
Russ Cox079c00a2008-11-17 12:34:03 -0800277
278 // Pick up a digit, put down a digit.
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800279 var n uint
Russ Cox079c00a2008-11-17 12:34:03 -0800280 for r--; r >= 0; r-- {
Robert Griesemerfaa9d1e2015-02-02 17:22:18 -0800281 n += (uint(a.d[r]) - '0') << k
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800282 quo := n / 10
283 rem := n - 10*quo
284 w--
Russ Cox3ee20852012-02-07 23:37:15 -0500285 if w < len(a.d) {
286 a.d[w] = byte(rem + '0')
287 } else if rem != 0 {
288 a.trunc = true
289 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800290 n = quo
Russ Cox079c00a2008-11-17 12:34:03 -0800291 }
292
293 // Put down extra digits.
294 for n > 0 {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800295 quo := n / 10
296 rem := n - 10*quo
297 w--
Russ Cox3ee20852012-02-07 23:37:15 -0500298 if w < len(a.d) {
299 a.d[w] = byte(rem + '0')
300 } else if rem != 0 {
301 a.trunc = true
302 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800303 n = quo
Russ Cox079c00a2008-11-17 12:34:03 -0800304 }
305
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800306 a.nd += delta
Russ Cox3ee20852012-02-07 23:37:15 -0500307 if a.nd >= len(a.d) {
308 a.nd = len(a.d)
309 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800310 a.dp += delta
311 trim(a)
Russ Cox079c00a2008-11-17 12:34:03 -0800312}
313
314// Binary shift left (k > 0) or right (k < 0).
Russ Coxcb51fdc2011-08-25 17:54:14 -0400315func (a *decimal) Shift(k int) {
Russ Cox079c00a2008-11-17 12:34:03 -0800316 switch {
Russ Coxcf9b7f72008-11-19 12:50:34 -0800317 case a.nd == 0:
318 // nothing to do: a == 0
Russ Cox079c00a2008-11-17 12:34:03 -0800319 case k > 0:
Russ Cox8a7cbad2009-01-15 17:22:17 -0800320 for k > maxShift {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800321 leftShift(a, maxShift)
322 k -= maxShift
Russ Cox079c00a2008-11-17 12:34:03 -0800323 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800324 leftShift(a, uint(k))
Russ Cox079c00a2008-11-17 12:34:03 -0800325 case k < 0:
Russ Cox8a7cbad2009-01-15 17:22:17 -0800326 for k < -maxShift {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800327 rightShift(a, maxShift)
328 k += maxShift
Russ Cox079c00a2008-11-17 12:34:03 -0800329 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800330 rightShift(a, uint(-k))
Russ Cox079c00a2008-11-17 12:34:03 -0800331 }
Russ Cox079c00a2008-11-17 12:34:03 -0800332}
333
334// If we chop a at nd digits, should we round up?
Russ Cox8a7cbad2009-01-15 17:22:17 -0800335func shouldRoundUp(a *decimal, nd int) bool {
Rob Pike64b6a782010-06-29 16:51:56 -0700336 if nd < 0 || nd >= a.nd {
Robert Griesemer40621d52009-11-09 12:07:39 -0800337 return false
Russ Cox079c00a2008-11-17 12:34:03 -0800338 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800339 if a.d[nd] == '5' && nd+1 == a.nd { // exactly halfway - round to even
Russ Cox3ee20852012-02-07 23:37:15 -0500340 // if we truncated, a little higher than what's recorded - always round up
341 if a.trunc {
342 return true
343 }
Rob Pike64b6a782010-06-29 16:51:56 -0700344 return nd > 0 && (a.d[nd-1]-'0')%2 != 0
Russ Cox079c00a2008-11-17 12:34:03 -0800345 }
346 // not halfway - digit tells all
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800347 return a.d[nd] >= '5'
Russ Cox079c00a2008-11-17 12:34:03 -0800348}
349
350// Round a to nd digits (or fewer).
Rob Pike64b6a782010-06-29 16:51:56 -0700351// If nd is zero, it means we're rounding
352// just to the left of the digits, as in
353// 0.09 -> 0.1.
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500354func (a *decimal) Round(nd int) {
Rob Pike64b6a782010-06-29 16:51:56 -0700355 if nd < 0 || nd >= a.nd {
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500356 return
Russ Cox079c00a2008-11-17 12:34:03 -0800357 }
Russ Coxa50cbf62009-06-24 20:12:50 -0700358 if shouldRoundUp(a, nd) {
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500359 a.RoundUp(nd)
360 } else {
361 a.RoundDown(nd)
Russ Cox079c00a2008-11-17 12:34:03 -0800362 }
Russ Cox079c00a2008-11-17 12:34:03 -0800363}
364
365// Round a down to nd digits (or fewer).
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500366func (a *decimal) RoundDown(nd int) {
Rob Pike64b6a782010-06-29 16:51:56 -0700367 if nd < 0 || nd >= a.nd {
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500368 return
Russ Cox079c00a2008-11-17 12:34:03 -0800369 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800370 a.nd = nd
371 trim(a)
Russ Cox079c00a2008-11-17 12:34:03 -0800372}
373
374// Round a up to nd digits (or fewer).
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500375func (a *decimal) RoundUp(nd int) {
Rob Pike64b6a782010-06-29 16:51:56 -0700376 if nd < 0 || nd >= a.nd {
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500377 return
Russ Cox079c00a2008-11-17 12:34:03 -0800378 }
379
380 // round up
Robert Griesemer3bb00322009-11-09 21:23:52 -0800381 for i := nd - 1; i >= 0; i-- {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800382 c := a.d[i]
383 if c < '9' { // can stop after this digit
384 a.d[i]++
385 a.nd = i + 1
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500386 return
Russ Cox079c00a2008-11-17 12:34:03 -0800387 }
388 }
389
390 // Number is all 9s.
391 // Change to single 1 with adjusted decimal point.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800392 a.d[0] = '1'
393 a.nd = 1
394 a.dp++
Russ Cox079c00a2008-11-17 12:34:03 -0800395}
396
397// Extract integer part, rounded appropriately.
398// No guarantees about overflow.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800399func (a *decimal) RoundedInteger() uint64 {
Russ Cox079c00a2008-11-17 12:34:03 -0800400 if a.dp > 20 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800401 return 0xFFFFFFFFFFFFFFFF
Russ Cox079c00a2008-11-17 12:34:03 -0800402 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800403 var i int
404 n := uint64(0)
Russ Cox079c00a2008-11-17 12:34:03 -0800405 for i = 0; i < a.dp && i < a.nd; i++ {
Robert Griesemer3bb00322009-11-09 21:23:52 -0800406 n = n*10 + uint64(a.d[i]-'0')
Russ Cox079c00a2008-11-17 12:34:03 -0800407 }
408 for ; i < a.dp; i++ {
Robert Griesemer40621d52009-11-09 12:07:39 -0800409 n *= 10
Russ Cox079c00a2008-11-17 12:34:03 -0800410 }
Russ Cox8a7cbad2009-01-15 17:22:17 -0800411 if shouldRoundUp(a, a.dp) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800412 n++
Russ Cox079c00a2008-11-17 12:34:03 -0800413 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800414 return n
Russ Cox079c00a2008-11-17 12:34:03 -0800415}