NumDigits returns the number of digits of the decimal coefficient (d.Value)
()
| 1237 | |
| 1238 | // NumDigits returns the number of digits of the decimal coefficient (d.Value) |
| 1239 | func (d Decimal) NumDigits() int { |
| 1240 | if d.value == nil { |
| 1241 | return 1 |
| 1242 | } |
| 1243 | |
| 1244 | if d.value.IsInt64() { |
| 1245 | i64 := d.value.Int64() |
| 1246 | // restrict fast path to integers with exact conversion to float64 |
| 1247 | if i64 <= (1<<53) && i64 >= -(1<<53) { |
| 1248 | if i64 == 0 { |
| 1249 | return 1 |
| 1250 | } |
| 1251 | return int(math.Log10(math.Abs(float64(i64)))) + 1 |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | estimatedNumDigits := int(float64(d.value.BitLen()) / math.Log2(10)) |
| 1256 | |
| 1257 | // estimatedNumDigits (lg10) may be off by 1, need to verify |
| 1258 | digitsBigInt := big.NewInt(int64(estimatedNumDigits)) |
| 1259 | errorCorrectionUnit := digitsBigInt.Exp(tenInt, digitsBigInt, nil) |
| 1260 | |
| 1261 | if d.value.CmpAbs(errorCorrectionUnit) >= 0 { |
| 1262 | return estimatedNumDigits + 1 |
| 1263 | } |
| 1264 | |
| 1265 | return estimatedNumDigits |
| 1266 | } |
| 1267 | |
| 1268 | // IsInteger returns true when decimal can be represented as an integer value, otherwise, it returns false. |
| 1269 | func (d Decimal) IsInteger() bool { |