MCPcopy Create free account
hub / github.com/shopspring/decimal / NumDigits

Method NumDigits

decimal.go:1239–1266  ·  view source on GitHub ↗

NumDigits returns the number of digits of the decimal coefficient (d.Value)

()

Source from the content-addressed store, hash-verified

1237
1238// NumDigits returns the number of digits of the decimal coefficient (d.Value)
1239func (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.
1269func (d Decimal) IsInteger() bool {

Callers 6

PowMethod · 0.95
PowWithPrecisionMethod · 0.95
ExpHullAbrhamMethod · 0.95
numDigitsFunction · 0.80
TestDecimal_NumDigitsFunction · 0.80
LnMethod · 0.80

Calls 1

AbsMethod · 0.80

Tested by 2

numDigitsFunction · 0.64
TestDecimal_NumDigitsFunction · 0.64