Reducing precision of numeric values?
- Leonid_RozenbergOP 1
If I have a
Decimal, what’s the best way to convert it to aNumeric 2?fromNumeric (roundCommercial 2 d)roundToNumeric RoundingDown (fromNumeric d)castAndRound dcast d
Option 4 is listed for completeness but one should not use it as it fails if
dhas more than 2 decimal places). Is option 1 performing a conversion to BigNumeric under the hood or similar to 3? Option 1, gives you more control over the rounding (one couldroundBankers), but not nearly as many options as the RoundingMode utilized by 2. - cocreature 2
Option 1 does not go via
BigDecimal. If the rounding modes are sufficient, that’s probably what I’d chose since it’s reasonably explicit about what you want to do and fairly efficient.Option 2 for when you need all the rounding modes.
3 for when you don’t care about being explicit wtr to rounding. Option 4 for when you don’t need rounding.