openexchangerates / accounting.js

A lightweight JavaScript library for number, money and currency formatting - fully localisable, zero dependencies.
http://openexchangerates.github.io/accounting.js
MIT License
4.95k stars 532 forks source link

Add option for rounding method #13

Open wjcrowcroft opened 13 years ago

wjcrowcroft commented 13 years ago

Rounding should be an option - always up, always down, or [default] nearest neighbour.

Can anybody weigh in on what the options for rounding would be labelled? floor and ceil possibly don't apply since they imply rounding up/down to integers, but rounding in our case also applies to floating points, eg 0.615 > 0.62

Any thoughts, do share. Cheers

mcordingley commented 12 years ago

Just a thought, since I've seen this before. You can do this first by scaling the number.

floor(0.615*100)/100

That will "round" 0.615 to 0.61.

mcordingley commented 12 years ago

Gah, that didn't format properly. Let's try it again

floor(0.615*100)/100 should return 0.61

mcordingley commented 12 years ago

Working on a solution in a branch. I'll let you know when I get somewhere interesting.

wjcrowcroft commented 12 years ago

This sounds like the way accounting.toFixed works, too, so have a look at that one. Would love to see anything you come up with.

mcordingley commented 12 years ago

Is there any particular reason why checkPrecision() forces a positive number? If we can use negative numbers, then we'll be able to round to the tens, hundreds, etc. I'm about to change it and want to be able to address any potential fallout. I saw it used in formatNumber for determining whether or not to add on a decimal, but nowhere else did it appear that a negative number would cause a break.

Thanks!

On Tue, Oct 18, 2011 at 1:16 PM, Joss Crowcroft reply@reply.github.com wrote:

This sounds like the way accounting.toFixed works, too, so have a look at that one. Would love to see anything you come up with.

Reply to this email directly or view it on GitHub: https://github.com/josscrowcroft/accounting.js/issues/13#issuecomment-2444667

Michael Cordingley 740.707.7809

mcordingley commented 12 years ago

Yep. I looked at accounting.toFixed as well. Pull request on this issue can be found here: https://github.com/josscrowcroft/accounting.js/pull/31