Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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 :/

 
Reply With Quote
 
 
 
 
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.


 
Reply With Quote
 
 
 
 
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 ***
 
Reply With Quote
 
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.

Read the newsgroup FAQ.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
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.
 
Reply With Quote
 
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.

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

http://www.crockford.com/javascript
 
Reply With Quote
 
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>

------------------------------------------------------------------------
 
Reply With Quote
 
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
 
Reply With Quote
 
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 ?


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


PointedEars
 
Reply With Quote
 
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 ?

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

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a simple CPU Design with some basic operations vhdl_addicted VHDL 4 08-31-2011 02:54 PM
a program of substraction Jenny C Programming 1 11-01-2008 09:25 AM
how to use io operations with open/save file dialog box in file menu for simple notepad boris Java 1 07-22-2007 04:13 PM
addition/substraction of pointers while passing to a function udayan.singh@gmail.com C Programming 4 01-20-2007 06:24 AM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM



Advertisments