-4

I need to increase a fraction in a text box using javascript.

the goal is to add 1 penny until it is 0.99 and then it will be 1.00 and 1.01

how can this be done in javascript?

this is what is not working.

var a;
a = document.getElementById('a1').value;
a = a+a;
alert(a);

alert returns

0.100.10

Additional Info

var a;
a = parseFloat(document.getElementById('b13').value);
a = a+a;
alert(a);

returns

0.2

i would rather see 0.20

but most importantly, how do increase this by 0.01 at a time ?

SOLVED:

both

var a;
a = parseFloat(document.getElementById('a1').value);
a = a+0.01;
alert(a);
}

and ...

document.getElementById('a1').value = +document.getElementById('a1').value + 0.01

worked fine.

4 Answers4

2

Text box returns the value as string so parse it

var a;
a = parseFloat(document.getElementById('a1').value);
a = (a+a).toFixed(2);
alert(a);

And it doesn't adds 0.01 to a .. It doubles the a so use something like this.

var a;
a = parseFloat(document.getElementById('a1').value);
a = (a + 0.01).toFixed(2);
alert(a);
Prasath K
  • 4,950
  • 7
  • 23
  • 35
  • 1
    I recommend that you add in the toFixed(2) from HMR's comment, so that 0.10 won't display as 0.1, and ideally, since the question was about changing a value in a textbox, put the way to set the value back after the calculation. – Scott Mermelstein May 21 '13 at 05:44
  • @ScottMermelstein Thank you .. Actually i didn't see the edited part of the question – Prasath K May 21 '13 at 05:46
2

First convert a into float like this,

a = parseFloat(document.getElementById('a1').value);

Then add and convert a to float like this,

a = (a + a).toFixed(2)

and alert the answer.

Suraj Jadhav
  • 632
  • 1
  • 5
  • 19
  • 1
    You're aware that `parseInt` will discard all decimals digits and `toFixed` will turn it into a string again, right? – Fabrício Matté May 21 '13 at 05:34
  • And that a = a + a doesn't add 0.01 to a... – Scott Mermelstein May 21 '13 at 05:35
  • You should use parseFloat instead of pareInt, I don't think the string that is returned matters much as the OP is going to use it for the value of a text input (most likely) but good to mention it if further processing is requiered. In the future we could use HTML 5 input type="number" – HMR May 21 '13 at 05:50
2

a = a+a doesn't add .01. It would double a if a was a number, but since javascript is seeing it as a string, it just concatenates it.

You want a = +a+0.01.

This will add 0.01 to the value of a. The initial + is to make sure that javascript treats a as a number instead of a string.

This will work fine for your alert, but then you still need to set the value back:

document.getElementById('a1').value = a

Or, to put it all in one line (using the toFixed addition from HMR):

document.getElementById('a1').value = (+document.getElementById('a1').value + 0.01).toFixed(2)
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • The OP updated the question at some point needing 2 decimal points, to do that you can do: (+"0.29" + 0.01).toFixed(2); This will return the string "0.30" – HMR May 21 '13 at 05:47
  • 1
    @HMR Funny, I was just leaving a comment to Prasath to do the same. I fixed it now. – Scott Mermelstein May 21 '13 at 05:49
1

a is containing a string, instead of a number. string + string returns the concatenation of the two strings - you haven't told Javascript it's a number, so it doesn't treat it like one.

You can use parseFloat and parseInt to turn strings into floating point numbers (have decimal places) or integers (do not). http://www.javascripter.net/faq/convert2.htm

However, be aware that floating point numbers have inaccuracies due to being stored in limited amount of memory - they will round off after a certain number of places (and not decimal places - binary places, for example 0.1 cannot be represented exactly as a floating point number, despite being only one decimal place in base 10!), and if you need to do important financial calculations, you should be aware of this inaccuracy (for example, you might use a fixed point number system instead). Read What Every Computer Scientist Should Know About Floating-Point Arithmetic for more information.

Patashu
  • 21,443
  • 3
  • 45
  • 53