decimial operations in javascript. best way to do a simple substraction ?

S

sonic

Hello,
What is the correct way of performing this substraction in JS.

var a = '29.33'; ( or 29.33 )
var b = '0.01'; ( or 0.01 )

var result = a - b;

( result = 29.319999999999996; )

my result is rounded off in the way floats would round off :/
 
R

Richard Cornford

sonic said:
What is the correct way of performing this
substraction in JS.

Using the subtraction operator.
var a = '29.33'; ( or 29.33 )
var b = '0.01'; ( or 0.01 )

var result = a - b;

Yes, that is the one.
( result = 29.319999999999996; )

my result is rounded off in the way floats would
round off :/

Javascript's one number type is a 64 bit IEEE double precision floating
point number.

Richard.
 
S

SoniC SouL

thats great. i just dont want the crazy rouding to appear. what do you
suggest i do to show the result as currency ?
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Mon, 2 May 2005 09:59:24, seen in
sonic said:
What is the correct way of performing this substraction in JS.

var a = '29.33'; ( or 29.33 )
var b = '0.01'; ( or 0.01 )

var result = a - b;

( result = 29.319999999999996; )

my result is rounded off in the way floats would round off :/

If those are Euros, and the inputs are meant to be exactly what you
wrote, then do the calculation in cents; or use StrU etc.

Read the newsgroup FAQ.
 
M

Michael Winter

thats great. i just dont want the crazy rouding to appear. what do you
suggest i do to show the result as currency ?

Perform the operations in pennies/cents/whatever so that you only use
integers. You can then use the function below to create a string
representing the value in pounds/dollars/whatever.

/* n - Number to format (in pennies).
* c - Currency symbol to use (defaults to none).
* t - Thousands separator (defaults to none).
* d - Decimal separator (defaults to '.').
*
* Outputs a number of the form cntnnntnnndnn
*
* For example, toCurrency(142635.7, '£', ',') produces
* £1,426.36
*/
function toCurrency(n, c, t, d) {
var s = (0 > n) ? '-' : ''; n = Math.abs(n);
var m = String(Math.round(n));
var j, i = '', f; c = c || ''; t = t || ''; d = d || '.';

while(m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while(j > 3) {
i = t + m.substring(j - 3, j) + i;
j -= 3;
}
i = m.substring(0, j) + i;
return s + c + i + d + f;
}

Notice the rounding of the floating-point value in the example.

Mike
 
D

Douglas Crockford

sonic said:
What is the correct way of performing this substraction in JS.

var a = '29.33'; ( or 29.33 )
var b = '0.01'; ( or 0.01 )

var result = a - b;

( result = 29.319999999999996; )

my result is rounded off in the way floats would round off :/

This is an inherent problem with floating-point arithmetic.
Floating-point should never be used when decimal precision is required,
such as in applications concerning money.

Use scaled integers instead. Instead of working in dollars, work in
cents. Integer arithmetic is precise.

http://www.crockford.com/javascript
 
P

Peter Wilson

sonic said:
Hello,
What is the correct way of performing this substraction in JS.

var a = '29.33'; ( or 29.33 )
var b = '0.01'; ( or 0.01 )

var result = a - b;

( result = 29.319999999999996; )

my result is rounded off in the way floats would round off :/
var result = (a-b).toFixed(2)

Pete
--
Peter Wilson
http://www.whitebeam.org
----


--
Peter Wilson
T: 01707 891840
M: 07796 656566
http://www.yellowhawk.co.uk
<http://www.yellowhawk.co.uk>

------------------------------------------------------------------------
 
R

Randy Webb

Peter said:
var result = (a-b).toFixed(2)

And then wonder what went wrong when tested in IE?

Read the group FAQ, explicitly section 4.6 with regards to that.
 
T

Thomas 'PointedEars' Lahn

SoniC said:
thats great. i just dont want the crazy rouding to appear. what do you
suggest i do to show the result as currency ?

I suggest you read the FAQ and improve your posting style.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 13 May
2005 21:08:39, seen in Thomas 'PointedEars'
Lahn said:
I suggest you read the FAQ and improve your posting style.

Citing a document and not indicating its location is unintelligent.

So is reviving a thread that came to a satisfactory conclusion a week
ago. You do not seem to realise that a future potential employer may do
an Internet search in order to investigate not only your technical
knowledge but also your personality and judgement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top