AdamWhiteHat / BigDecimal

An arbitrary-precision decimal (base 10) floating-point number class.
MIT License
47 stars 15 forks source link

Request: round - add support for negative precision #37

Closed Bykiev closed 4 months ago

Bykiev commented 4 months ago

Proposal/Summary Add support for negative precision to behave like ROUNDDOWN Excel function

Rationale It will be useful to for rounding down to the left of the decimal point

AdamWhiteHat commented 4 months ago

So what you are asking for is

the functionality of =ROUNDDOWN(31415.92654, -2)

Which rounds 31415.92654 down to 2 decimal places to the left of the decimal point, resulting in 31400

By allowing BigDecimal Round(BigDecimal value, Int32 precision) to accept negative values on the precision parameter?

Bykiev commented 4 months ago

Yes, you're right

AdamWhiteHat commented 4 months ago

This is done and should be working.

I have published an updated NuGet package with this feature and all of your bug fixes you have contributed: ExtendedNumerics.BigDecimal ver. 2025.1000.0.118

Thank you for your contributions and interest in my library.

Protiguous commented 4 months ago

For any bugs that are noticed, are there tests being written for them also?

AdamWhiteHat commented 4 months ago

For any bugs that are noticed, are there tests being written for them also?

Actually, I did check in tests for this new functionality, just in the commit prior to this one: https://github.com/AdamWhiteHat/BigDecimal/commit/f19fd65a0378ad537d5ff18477cf1478f732462c?diff=split&w=1#diff-8e5528d420d41e964bb8abcb89ae001e61e72506ac2f722a7058d969886e4778R217

AdamWhiteHat commented 4 months ago

@Bykiev Are you satisfied here? Can I close this request?

Bykiev commented 4 months ago

@AdamWhiteHat , hi, thank you for your inputs. It doesn't seems to be accurate val = 88.123459; p = -3; results to System.ArgumentOutOfRangeException, should be 0

AdamWhiteHat commented 4 months ago

val = 88.123459; p = -3; results to System.ArgumentOutOfRangeException, should be 0 Try p = 2.

I wasn't anticipating people trying to pass in a negative precision greater than there are place values to move the decimal point. Well, I mean I did anticipate they might, I just choose to throw an exception in that case.

But I did just check the actual excel function, and it does seem to accept large negative value than there are digits without error: image

So I guess I will change it to make it match Excel's behavior.

AdamWhiteHat commented 4 months ago

A new NuGet package, version 2025.1000.2.122 is now available.

Bykiev commented 4 months ago

Thank you, works great!