Would this be a reliable way to deal with rounding errors when multiplying floating point numbers in Javascript? -



Would this be a reliable way to deal with rounding errors when multiplying floating point numbers in Javascript? -

i adding client-side sub-total calculations order page, volume discount show user makes selections.

i finding of calculations off 1 cent here or there. wouldn't big deal except fact total doesn't match final total calculated server-side (in php).

i know rounding errors expected result when dealing floating point numbers. example, 149.95 * 0.15 = 22.492499999999996 , 149.95 * 0.30 = 44.98499999999999. former rounds desired, latter not.

i've searched on topic , found variety of discussions, nil satisfactorily addresses problem.

my current calculation follows:

discount = math.round(price * factor * 100) / 100;

a mutual suggestion work in cents rather fractions of dollars. however, require me convert starting numbers, round them, multiply them, round result, , convert back.

essentially:

discount = math.round(math.round(price * 100) * math.round(factor * 100) / 100) / 100;

i thinking of adding 0.0001 number before rounding. example:

discount = math.round(price * factor * 100 + 0.0001) / 100;

this works scenarios i've tried, wondering logic. adding 0.0001 enough, , never much, forcefulness desired rounding result?

note: purposes here, concerned single calculation per cost (so not compounding errors) , never displaying more 2 decimal places.

edit: example, want round result of 149.95 * 0.30 2 decimal places , 44.99. however, 44.98 because actual result 44.98499999999999 not 44.985. error not beingness introduced / 100. happening before that.

test:

alert(149.95 * 0.30); // yields 44.98499999999999

thus:

alert(math.round(149.95 * 0.30 * 100) / 100); // yields 44.98

the 44.98 expected considering actual result of multiplication, not desired since not user expect (and differs php result).

solution: i'm going convert integers calculations. accepted reply points out, can simplify original conversion calculation somewhat. thought of adding 0.0001 dirty hack. best utilize right tool job.

i don't think adding little amount favor you, guess there cases much. needs documented, otherwise 1 see incorrect.

working in cents […] require me convert starting numbers, round them, multiply them, round result, , convert back:

discount = math.round(math.round(price * 100) * math.round(factor * 100) / 100) / 100;

i think should work round afterwards only. however, should first multiply result significant digits sum of 2 sig digits before, i.e. 2+2=4 decimal places in example:

discount = math.round(math.round( (price * factor) * 10000) / 100) / 100;

javascript floating-point rounding multiplication

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -