elm-round icon indicating copy to clipboard operation
elm-round copied to clipboard

Suggestion: function which takes existing number of decimal places into account

Open kfrn opened this issue 7 years ago • 3 comments

Thanks for this very helpful library!

I wonder if you'd consider a function that takes the number of decimal places to round to - but if the float has fewer decimal places that that number, then the existing float is simply returned.
This would be nice for when you are working with numbers with a variable number of decimal places, avoiding unnecessary zero-padding.

Current behaviour:

round 6 0.123456789"0.123457"

round 6 0.123"0.123000"

Desired behaviour of potential function roundMax:

roundMax 6 0.123456789"0.123457"

roundMax 6 0.1234"0.1234"

I implemented it myself in a basic way (by comparing string lengths), but it could be nice as a built-in.

Thanks very much!

kfrn avatar May 10 '18 06:05 kfrn

Thank you for the suggestion. I think this should be the default behaviour. One could pad the result with zeros if needed using String.padRight easily.

I'll have a look!

Am 10. Mai 2018 08:14:01 MESZ schrieb Katherine Frances Nagels [email protected]:

Thanks for this very helpful library!

I wonder if you'd consider a function that takes the number of decimal places to round to - but if the float has fewer decimal places that that number, then the existing float is simply returned.
This would be nice for when you are working with numbers with a variable number of decimal places, avoiding unnecessary zero-padding.

Current behaviour:

round 6 0.123456789"0.123457"

round 6 0.123"0.123000"

Desired behaviour of potential function roundMax:

roundMax 6 0.123456789"0.123457"

roundMax 6 0.1234"0.1234"

I implemented it myself in a basic way (by comparing string lengths), but it could be nice as a built-in.

Thanks very much!

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/myrho/elm-round/issues/4

--

myrho avatar May 10 '18 20:05 myrho

Cheers for the response, @myrho.

As for default behaviour, you could argue either way, I think: not padding v. having a consistent number of decimal places.

Using String.pad to add the zeros might not be so straightforward - it would only work predictably if the integer part of the number (i.e. the part before the decimal place) was of a constant length.
Therefore it would be nice to have both options available!

kfrn avatar May 10 '18 22:05 kfrn

Sorry for the late reply!

Yes, I agree, it's probably not that easy. I now think, the easiest way would be to add one more function like removeDanglingZeros which just removes dangling zeros. So the current API remains unchanged and all rounding methods and variants (ceil, floor, roundCom, ...) are covered.

myrho avatar May 31 '18 20:05 myrho