openplanet-nl / issues

Issue tracker for Openplanet.
10 stars 0 forks source link

`Math::Round(float)` does not support rounding to a custom number of decimal spaces #536

Closed CodyNinja1 closed 1 month ago

CodyNinja1 commented 1 month ago

Currently, you can't (for example) use Math::Round(float) to round a number to only 2 decimal spaces, or any number for that matter.

Example of how to fix this:

void Main()
{
    float ExampleFloat = 3.14159;
    print(Math::Round(ExampleFloat));     // 3.00
    print(Math::Round(ExampleFloat, 2));     // 3.14
    print(Math::Round(ExampleFloat, 4));     // 3.1416
}
XertroV commented 1 month ago

You can like this: float y = Math::Round(x * 100.0) * 0.01;

Phlarx commented 1 month ago

Maybe personal opinion, but I think Math::Round is often not the right thing to use anyway. It should only be used if you're then using the rounded value for subsequent logic.

Text::Format should be used in all other cases, e.g. Text::Format("%#3.1f%%", progressPercentage)

codecat commented 1 month ago

Does the format solution work for your use case, @CodyNinja1?

CodyNinja1 commented 1 month ago

Maybe personal opinion, but I think Math::Round is often not the right thing to use anyway. It should only be used if you're then using the rounded value for subsequent logic.

I am using it to perform mathematical operations on the rounded number

CodyNinja1 commented 1 month ago

Does the format solution work for your use case, @CodyNinja1?

I could use the format solution to convert the float to string with format, and then back to float but that feels clunky. I also could use XertroV's solution of adding extra zeros and removing them from the float after rounding, but I it would be prefered if that functionality was built into Math::Round

codecat commented 1 month ago

I see - what's your use case specifically? I'm not against this, by the way. Just wondering!

CodyNinja1 commented 1 month ago

Working with floats can be a challenge sometimes, and a good way of removing float imprecision is rounding by 2 or 3 digits.

XertroV commented 1 month ago

How do you figure that?

I'd guess most (mathematically literate) people would agree that rounding introduces imprecision.

codecat commented 1 month ago

a good way of removing float imprecision is rounding by 2 or 3 digits.

Like Xertrov says, I don't think this is true. But then again, I don't know your use-case.