decimal icon indicating copy to clipboard operation
decimal copied to clipboard

More detailed explanation for NewFromFloatWithExponent

Open GCrispino opened this issue 2 years ago • 0 comments

Hello!

Current comment for func NewFromFloatWithExponent(value float64, exp int32) Decimal function is comprised of the following:

NewFromFloatWithExponent converts a float64 to Decimal, with an arbitrary number of fractional digits.

This is then followed by this example:

NewFromFloatWithExponent(123.456, -2).String() // output: "123.46"

While this is accurate, it doesn't feel clear to me what exactly exp is doing to the value variable. More specifically, what does the -2 value does to the 123.456 given as input? Does this round 123.456 up to two decimal points? What if it was 2 instead of -2?

Maybe explaining more about the example or about the function itself could be valuable here.

I might be wrong and this might be obvious somehow, but for me as a person with not a lot of background on floating point arithmetic, it felt this way. I'd also happily add more details on it as examples, but I'm not sure if I have enough knowledge for that.

Although with a different goal, I feel this is similar to this issue https://github.com/shopspring/decimal/issues/79.

Thanks!

GCrispino avatar Mar 10 '22 02:03 GCrispino