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

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

sonic
Guest
Posts: n/a

 05-02-2005
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 :/

Richard Cornford
Guest
Posts: n/a

 05-02-2005
sonic wrote:
> 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.

SoniC SouL
Guest
Posts: n/a

 05-02-2005

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

*** Sent via Developersdex http://www.developersdex.com ***

Dr John Stockton
Guest
Posts: n/a

 05-02-2005
JRS: In article <(E-Mail Removed) .com>
, dated Mon, 2 May 2005 09:59:24, seen in news:comp.lang.javascript,
sonic <(E-Mail Removed)> posted :

>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.

--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

Michael Winter
Guest
Posts: n/a

 05-02-2005
On 02/05/2005 19:12, SoniC SouL wrote:

> 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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Douglas Crockford
Guest
Posts: n/a

 05-02-2005
sonic wrote:
> 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.

cents. Integer arithmetic is precise.

http://www.crockford.com/javascript

Peter Wilson
Guest
Posts: n/a

 05-03-2005
sonic wrote:
> 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>

------------------------------------------------------------------------

Randy Webb
Guest
Posts: n/a

 05-04-2005
Peter Wilson wrote:

> sonic wrote:
>
>> 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)

And then wonder what went wrong when tested in IE?

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

--
Randy

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 05-13-2005
SoniC SouL wrote:

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

PointedEars

Dr John Stockton
Guest
Posts: n/a

 05-14-2005
JRS: In article <(E-Mail Removed)>, dated Fri, 13 May
2005 21:08:39, seen in news:comp.lang.javascript, Thomas 'PointedEars'
Lahn <(E-Mail Removed)> posted :
>SoniC SouL wrote:
>
>> thats great. i just dont want the crazy rouding to appear. what do you
>> suggest i do to show the result as currency ?

>

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.

--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.