By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

it won't parse float! help please

P: 47
ok, i'm trying to get a return value of a float...... but it keeps giving me a load of numbers!

how do i make this interger behave?

the problem is line 39. thanks.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="javascript1.2">
  3.  
  4.  
  5. function checkreset()
  6. {
  7.  
  8.    if (confirm('Are you sure you wish to clear the form?'))
  9.    {
  10.       return true
  11.    }
  12.    return false
  13. }
  14.  
  15.  
  16. function addnumbers()
  17. {
  18.  
  19. // This function reads the values from the input boxes
  20. // parseFloat converts the input boxes into an integer
  21.  
  22. var x,y,result;
  23. a = parseFloat(document.form1.abox.value);
  24. b = parseFloat(document.form1.bbox.value);
  25. c = parseFloat(document.form1.cbox.value);
  26. d = parseFloat(document.form1.dbox.value);
  27. e = parseFloat(document.form1.ebox.value);
  28. f = parseFloat(document.form1.fbox.value);
  29. g = parseFloat(document.form1.gbox.value);
  30. h = parseFloat(document.form1.hbox.value);
  31. i = parseFloat(document.form1.ibox.value);
  32. j = parseFloat(document.form1.jbox.value);
  33.  
  34. // Then we do the math
  35.  
  36. result = a + b + c + d + e + f + g + h + i + j;
  37.  
  38. // And write the result straight to the screen
  39. document.form1.qbox.value = result;
  40. document.form1.totalbox.value = result*10.21;
  41.  
  42.  
  43. // As the function has already done the output, we don’t need a return value
  44. }
  45. </script>
  46.  
  47.  
Oct 26 '07 #1
Share this Question
Share on Google+
7 Replies


gits
Expert Mod 5K+
P: 5,390
hi ...

nope ... parseFloat() handles the values as float :) ... you may use parseInt() instead ... and multiplying a float produces float again ... so you may use the math-methods to round the value

kind regards
Oct 26 '07 #2

P: 47
ok, i tried math.round, and that just rounds up the number, which is no good.

so for example, instead of getting a result of €30.630000000000002, i'd like to just get a result of €30.63

how do i do this?

thanks
Oct 30 '07 #3

P: 47
i'm multiplying a number by 10.21 in javascript,

Expand|Select|Wrap|Line Numbers
  1. document.form1.totalbox.value = "€" + result*10.21;
  2.  
how do i remove the extra zeros?

for example, instead of getting a result of €30.630000000000002, i'd like to just get a result of €30.63

thanks
Oct 30 '07 #4

gits
Expert Mod 5K+
P: 5,390
use the toFixed(); method instead ...

kind regards
Oct 30 '07 #5

gits
Expert Mod 5K+
P: 5,390
threads merged ... please don't double post questions ...

kind regards
Oct 30 '07 #6

P: 47
threads merged ... please don't double post questions ...

kind regards
no worries, that worked, thanks.

Expand|Select|Wrap|Line Numbers
  1. document.form1.totalbox.value = "€" + (result*10.21).toFixed(2);
  2.  
Oct 30 '07 #7

gits
Expert Mod 5K+
P: 5,390
:) glad to hear that ... post back to the forum anytime you have more questions

kind regards
Oct 30 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.