Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > HTML, XML, Javascript, AJAX
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2002, 02:20 AM   #1 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
Floating away...

has anybody had experience with float calculation ( or just plain decimals ) with javascript...

here is an example:

Code:
<html> <body> <input type="text" name="abx" value="800" /> <input type="text" name="abz" value="1.12" /> <script language="javascript"> function testIt(){ var f1 = parseFloat(abx.value); var f2 = parseFloat(abz.value); var re = f1*f2; alert("result: " + re); } </script> <input type="button" onclick="testIt();" value="Click Me" /> </body> </html>
you would, of course, expect to see "896.0" as the answer..

however if you try it you will get "896.00000000000001" ...

anyone know whats up with this and / or a mathematical work-around?
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 11-21-2002, 11:45 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
i found a handy script for formatting numbers .. kinda long, but it works.
Code:
<html> <body> <input type="text" name="abx" value="800" /> <input type="text" name="abz" value="1.12" /> <SCRIPT language="JavaScript"> <!-- Hide from older browsers // Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au // Please acknowledge use of this code by including this header. // CONSTANTS var separator = ","; // use comma as 000's separator var decpoint = "."; // use period as decimal point var percent = "%"; var currency = "$"; // use dollar sign for currency function formatNumber(number, format, print) { // use: formatNumber(number, "format") if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>"); if (number - 0 != number) return null; // if number is NaN return null var useSeparator = format.indexOf(separator) != -1; // use separators in number var usePercent = format.indexOf(percent) != -1; // convert output to percentage var useCurrency = format.indexOf(currency) != -1; // use currency format var isNegative = (number < 0); number = Math.abs (number); if (usePercent) number *= 100; format = strip(format, separator + percent + currency); // remove key characters number = "" + number; // convert number input to string // split input value into LHS and RHS using decpoint as divider var dec = number.indexOf(decpoint) != -1; var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number; var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : ""; // split format string into LHS and RHS using decpoint as divider dec = format.indexOf(decpoint) != -1; var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format; var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : ""; // adjust decimal places by cropping or adding zeros to LHS of number if (srightEnd.length < nrightEnd.length) { var nextChar = nrightEnd.charAt(srightEnd.length) - 0; nrightEnd = nrightEnd.substring(0, srightEnd.length); if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1); // round up // patch provided by Patti Marcoux 1999/08/06 while (srightEnd.length > nrightEnd.length) { nrightEnd = "0" + nrightEnd; } if (srightEnd.length < nrightEnd.length) { nrightEnd = nrightEnd.substring(1); nleftEnd = (nleftEnd - 0) + 1; } } else { for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) { if (srightEnd.charAt(i) == "0") nrightEnd += "0"; // append zero to RHS of number else break; } } // adjust leading zeros sleftEnd = strip(sleftEnd, "#"); // remove hashes from LHS of format while (sleftEnd.length > nleftEnd.length) { nleftEnd = "0" + nleftEnd; // prepend zero to LHS of number } if (useSeparator) nleftEnd = separate(nleftEnd, separator); // add separator var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : ""); // combine parts output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : ""); if (isNegative) { // patch suggested by Tom Denn 25/4/2001 output = (useCurrency) ? "(" + output + ")" : "-" + output; } return output; } function strip(input, chars) { // strip all characters in 'chars' from input var output = ""; // initialise output string for (var i=0; i < input.length; i++) if (chars.indexOf(input.charAt(i)) == -1) output += input.charAt(i); return output; } function separate(input, separator) { // format input using 'separator' to mark 000's input = "" + input; var output = ""; // initialise output string for (var i=0; i < input.length; i++) { if (i != 0 && (input.length - i) % 3 == 0) output += separator; output += input.charAt(i); } return output; } function testIt(){ var f1 = parseFloat(abx.value); var f2 = parseFloat(abz.value); var re = f1*f2; var n = formatNumber(re, "##0.#") alert("result: " + n); } // Stop hiding --> </script> <input type="button" onclick="testIt();" value="Click Me" /> </body> </html>
here is a page with examples of formats: http://members.ozemail.com.au/~dcrombie/format.html
__________________
sde is offline   Reply With Quote
Old 11-21-2002, 03:05 PM   #3 (permalink)
anon
 
Posts: n/a
Er, Didnt really say what was the problem that needed to be solved... to just get 896 do parseInt(re)

Or just do

Code:
document.write("result : " + (parseInt(re*1000))/1000);
That should work to keep 3 the number to decimal places...

Lets say we had 123.4567 It would multiply by a thousand and we would get 123456.7, parseInt it. we have 123456, divide 123.456 . Done
__________________
  Reply With Quote
Old 11-21-2002, 03:41 PM   #4 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
actually i wanted it to a "fixed" two decimal value of 100000 of a cent accuracy, this is how i did it:

Code:
function t2(v){ tmpv = v; suffix = "" do0 = false; if(Math.round(v) == v){ suffix = ".00"; res = v+suffix; return res; }else{ v = v+""; tmpy = v.indexOf("."); if(tmpy+6 < v.length){ tmpRem = v.substring(tmpy, tmpy+6); tmpz = Math.floor(v); return tmpz+".00"; } } tmpv = tmpv * 100; tmpv = Math.round(tmpv); if(tmpv % 10 == 0 || tmpv % -10 == 0){ do0 = true; } tmpv = tmpv / 100; if(do0){ tmpv = tmpv + "0"; } return tmpv; }
unfortunately, or fortunately, after writing that i discovered this function:

Code:
var x = 16.000000000000001; alert(x.toFixed(2));
hmm, oh well at least that was only a small section of what i had to do, the code wasn't a waste.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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

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



All times are GMT -8. The time now is 06:48 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle