Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 26th, 2008, 12:11 AM
Registered User
 
Join Date: May 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with JavaScript function parseFloat

Hi,

I have a problem with JavaScript function parseFloat.

when i am trying to convert a string which contains 15 digits and 2 decimal points("123456789012345.09") to number type using parseFloat function, it returns value wrongly like 123456789012345.1

eg:
var V1 = parseFloat("123456789012345.09");
here V1 value will be 123456789012345.1

like above parseFloat("123456789012345.99") will return 123456789012345.98
      parseFloat("123456789012345.96") will return 123456789012345.95

parseFloat function works wrongly if the number exceeds 14 digits with 2 decimal points, else it works fine.

i am facing this problem for some Scenarios, not for all conversions.

Need your help to solve this issue.

 
Old May 26th, 2008, 04:02 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

That's probably a combination of the accuracy of the numeric functions and also some values are not expressible exactly in binary. My approach would be to multiply the initial string by 100, this will try to cast the string as a number. If the result is NaN then it wasn't valid. What you do next depends on what you actually do with the number.

--

Joe (Microsoft MVP - XML)
 
Old May 26th, 2008, 07:06 AM
Registered User
 
Join Date: May 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi joefawcett,

In my application(ASP.net), the user will be entering the numeric values(like 123456789012345.09) in textbox, in some events(like onblur of textbox, onClick event on button) i will be capturing the textbox value which will be in string format and will convert to numeric type using parseFloat function to do some calculation.

i will be dealing with maximum of 8 decimal points.

also parseFloat("123456789012345.229") will return 123456789012345.23
     parseFloat("123456789012345.203") will return 123456789012345.2

as per your suggestion, how we can multiply the string with 100 or other in javascript?

Need your help to solve this issue.


 
Old May 26th, 2008, 07:49 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I slightly misunderstood but I think you are simply running up against the limitations of JavaScript's arithmetic precision. See http://www.jibbering.com/faq/#FAQ4_7. There's probably a workaround, what do these figures represent? Can you move the logic to the server where a language such as C# will probably be more accurate?

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
stop current javascript function using javascript sakthi Javascript 3 June 2nd, 2008 03:30 PM
Number or parseFloat crmpicco Javascript How-To 6 March 21st, 2006 01:18 PM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 06:03 AM
get value from javascript function suzila ASP.NET 1.0 and 1.1 Basics 6 December 3rd, 2005 10:38 AM
Problem with a Javascript function SuMajestad PHP How-To 4 August 17th, 2004 09:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.