473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Jul 23 '05 #1
9 2304
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.
Jul 23 '05 #2


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 ***
Jul 23 '05 #3
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.
Jul 23 '05 #4
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
Jul 23 '05 #5
JRS: In article <11**********************@o13g2000cwo.googlegroups .com>
, dated Mon, 2 May 2005 09:59:24, seen in news:comp.lang.javascript,
sonic <so*******@gmail.com> 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.
Jul 23 '05 #6
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>

------------------------------------------------------------------------
Jul 23 '05 #7
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
Jul 23 '05 #8
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
Jul 23 '05 #9
JRS: In article <18****************@PointedEars.de>, dated Fri, 13 May
2005 21:08:39, seen in news:comp.lang.javascript, Thomas 'PointedEars'
Lahn <Po*********@web.de> 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.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is...
6
by: luckyjameel | last post by:
Hi Guys Hope this message shall find u all in gr8 moods. As i was developing a Application for Online Exam and i am in need of a way to maintain the time for the logged in user. So Can any one...
9
by: optimistx | last post by:
Which url in your opinion would be a good or even the best example of javascript usage in a set of pages at least say 10 or more pages? How to use css, how to split js-code to files, how to code...
36
by: mrby | last post by:
Hi, Does anyone know of any link which describes the (relative) performance of all kinds of C operations? e.g: how fast is "add" comparing with "multiplication" on a typical machine. Thanks!...
16
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
8
by: ianenis.tiryaki | last post by:
well i wrote this code but it doesn't work and shows me bunch of syntax if you can help me out finding them i would appreciate it! the program should do simple calculations and no parenthesis...
11
by: test | last post by:
Hi, I'm very new to Javascript so maybe my question could be stupid. I'm playing with microcontrollers and a TCP/IP component. This means that I can control electronics via TCP/IP, UDP and so on....
7
by: Jenny | last post by:
/*I made this program to do substraction between two integers whose digits are beyond the limit of a long int on my system but the program can only execute within 10 digits.What's more only under...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.