473,414 Members | 1,703 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,414 software developers and data experts.

Trouble whit praseFloat() function.

Code:

<SCRIPT language="JavaScript">
Rente = "5,9";
Rente2 = parseFloat(Rente.replace(",",".")/100);
document.write(Rente2 + '<br>');

Rente = "4,9";
Rente2 = parseFloat(Rente.replace(",",".")/100);
document.write(Rente2 + '<br>');
</SCRIPT>

Result:
0.059000000000000004
0.049

Problem:

Where does the "000000000000004" come from? Are there a workaround?

- Espen


May 26 '06 #1
3 2141
Espen said the following on 5/26/2006 11:16 AM:
Code:

<SCRIPT language="JavaScript">
Rente = "5,9";
Rente2 = parseFloat(Rente.replace(",",".")/100);
document.write(Rente2 + '<br>');
Rente = "4,9";
Rente2 = parseFloat(Rente.replace(",",".")/100);
document.write(Rente2 + '<br>');
</SCRIPT>
Result:
0.059000000000000004
0.049
Problem:

Where does the "000000000000004" come from? Are there a workaround?


Yes, don't use parseFloat when you need precision. Search the archives,
it has been discussed so much that it makes one sick reading it all. The
error comes in when converting from decimal to binary and back to decimal.

Do your math with integers, then convert it to decimal using String
operations.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 26 '06 #2
Espen wrote on 26 mei 2006 in comp.lang.javascript:
<SCRIPT language="JavaScript">
Do:
<script type='text/javascript'>
Rente = "5,9";
Rente2 = parseFloat(Rente.replace(",",".")/100);
document.write(Rente2 + '<br>');
Why would you first devide by 100,
and then, the result being numeric or has errored out,
doe a parseFloat() ???????????

Rente = "4,9";
Rente2 = parseFloat(Rente.replace(",",".")/100);
document.write(Rente2 + '<br>');
</SCRIPT>

Result:
0.059000000000000004
0.049
Problem:
Where does the "000000000000004" come from? Are there a workaround?


This, Espen, has been told hundreds of times on this NG, [see the
archive]:

because some binary coded floating point values canot be exactly
converted to decimal fractions and others can.

There are only two ways, apart from having a language that stores decimal
fractions:

1 do decent rounding and a final string manipulation

2 use only integers and display them with a decimal point with string
manipulation.

the NG-FAQ
<http://www.jibbering.com/faq/>
says:

Javascript numbers are represented in binary as IEEE-754 Doubles, with a
resolution of 53 bits, giving an accuracy of 15-16 decimal digits;
integers up to about 9e15 are precise, but few decimal fractions are.
Given this, arithmetic is as exact as possible, but no more. Operations
on integers are exact if the true result and all intermediates are
integers within that range.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 26 '06 #3
JRS: In article <12*************@corp.supernews.com>, dated Fri, 26 May
2006 17:16:49 remote, seen in news:comp.lang.javascript, Espen
<sp**@onlime.org> posted :
Code:

<SCRIPT language="JavaScript">
Rente = "5,9";
Rente2 = parseFloat(Rente.replace(",",".")/100);
document.write(Rente2 + '<br>'); ... Where does the "000000000000004" come from? Are there a workaround?


Earlier post with non-equivalent Subject but equivalent body already
answered in news:no.c.l.j .

--
© 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.
May 26 '06 #4

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

Similar topics

1
by: Admir Hod?ic via AccessMonster.com | last post by:
I use some old fashion program whit DBF files and now i step by step moving thth to acces. Problem occurs when i try to link DBF tables whit MEMO FILED wich requvest file whit ''name''.ftp extension...
3
by: VKTOP | last post by:
Hi friends! I have a problem i have a textbox in aspx webform: in the load event : txtNombre.Attributes.Add("onblur", "CambiarMayusculas(' txtNombre')") i have a javascript function : ...
2
by: saeed rezaei | last post by:
hi i am saeed rezaei srezaii2000@yahoo.com who is knowh how i can changing the excel cells backcolor. now i can working whit excel but still i cant change the backcolor of cells.
1
by: stefano | last post by:
Hi, I have a file xml (p.xml) and I use XSLT to trasform p.xml to XHTML. this is the xml file: .... <test label="x"> <itemList> <testItem oknumber="2" > .... </testItem> </itemList>
1
by: news.wanadoo.nl | last post by:
Hi, I have found whit google this form in this group. But i'n not good in javascript :(. I try and error normal but i only error now :P Whit i now want to now how can i get access to the vars...
14
by: Wouter | last post by:
Hi, I try to make the follow. I want that i can click on a text link and that then a link wil be copyed in a input form box (<input type="text" name="img_url" />). I have google-ed about how...
2
by: azgar | last post by:
Hello, i Want now how lock or block an User on Active Directory using the DirectoryEntry component or another way in .Net. Thanks
13
by: oruccim | last post by:
hii my friends; I want to create picture of security code.can i do it ? if yes,how , which module will help me? have you got a example that can create a picture whit my name pls help me thansk...
12
by: viki1967 | last post by:
Good afternoon to everyone. I write this page htm with JavaScript code: <script language="javascript" type="text/javascript"> <!-- function parseNumber(val) {
5
by: viki1967 | last post by:
Hi all. I need one example for dynamic box whit Ajax and ASP. Can you help me ? Thanks Viki
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.