473,698 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble whit praseFloat() function.

Code:

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

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

Result:
0.0590000000000 00004
0.049

Problem:

Where does the "00000000000000 4" come from? Are there a workaround?

- Espen


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

<SCRIPT language="JavaS cript">
Rente = "5,9";
Rente2 = parseFloat(Rent e.replace(","," .")/100);
document.write( Rente2 + '<br>');
Rente = "4,9";
Rente2 = parseFloat(Rent e.replace(","," .")/100);
document.write( Rente2 + '<br>');
</SCRIPT>
Result:
0.0590000000000 00004
0.049
Problem:

Where does the "00000000000000 4" 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.javas cript 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.javas cript:
<SCRIPT language="JavaS cript">
Do:
<script type='text/javascript'>
Rente = "5,9";
Rente2 = parseFloat(Rent e.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(Rent e.replace(","," .")/100);
document.write( Rente2 + '<br>');
</SCRIPT>

Result:
0.0590000000000 00004
0.049
Problem:
Where does the "00000000000000 4" 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.c om/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.supernew s.com>, dated Fri, 26 May
2006 17:16:49 remote, seen in news:comp.lang. javascript, Espen
<sp**@onlime.or g> posted :
Code:

<SCRIPT language="JavaS cript">
Rente = "5,9";
Rente2 = parseFloat(Rent e.replace(","," .")/100);
document.write (Rente2 + '<br>'); ... Where does the "00000000000000 4" 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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
1609
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 whic reprezent that memo filed. That kind of DBF file i cant link or import. Anyone have fix for that is there any program to i can delete that filed before i link table or is there any good option for that. thanx a lot sory on my bed english --...
3
991
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 : function CambiarMayusculas(elemento1) {var elem=document.getElementById(elemento1); if (elem.value!="")
2
1489
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
1407
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
1977
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 vis and inv. Normal when i submit a form then is see something link formhandler?vis=somthing&inv=something. But i dont see that in this form. I want the use data form this form to set it in a databse whit php. I cant get use nog $_GET is wil give...
14
3271
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 i can make this but i cant find a explane how i can do this. I'm sure my javascript skills are the problem.... Is there here someone who can help me whit this ?
2
1285
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
1522
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 oruc
12
1518
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
2172
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
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7739
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.