Jump to content

Some JS maths help please!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
jaydee

jaydee

    Newbie

  • Members
  • PipPip
  • 17 posts
Hey,

Pretty new to Javascript so yeah :)

Right basically I have this variable in a function:

a = document.getElementById('amnt22').innerHTML

This is a number variable, starts off at 0.10 (10 cents) and the function is called upon clicking of a button, now, I need to add 0.10 everytime the button is clicked, I've played around with all sorts, toFixed, parseInt, parseFloat etc etc all have weird results or it just doesn't work at all. Like 0.001.10 on some of them! :S

Spent hours and hours it's starting to get slightly annoying now, so basically yeah I need it to go up in increments of 0.10 so like 0.10+0.10 = 0.20 this would go on into the $1 plus... 0.10, 0.20, 0.30... 1.20 etc.

I need to set the innerHTML as it obviously so can anyone help me out!?

Thanks a lot in advance for any replies :)

#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Doesn't something like this work?

a = parseInt(document.getElementById('amnt22').innerHTML);
a = a + 0.1;
document.getElementById('amnt22').innerHTML = a;


#3
jaydee

jaydee

    Newbie

  • Members
  • PipPip
  • 17 posts
For some reason, it works only once. I think it's to do with the parseInt, cos it's a decimal number it's taking the decimal part away. I tried it with alert in between and the popup showed zero. Which is why I used all the other methods that still failed, for something so simple it's causing a lot of problems!

#4
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Yeah, sorry I meant parseFloat. So that one doesn't work either... Are you sure it's when you add 0.1 it's goes wrong? You could try a = a - -0.1; just to check.

#5
jaydee

jaydee

    Newbie

  • Members
  • PipPip
  • 17 posts
Something else weird happens with that, it works, but only on ones it wants to, I click it 6 times here's the results:
1. 0.2
2. 0.30000000000000004
3. 0.4
4. 0.5
5. 0.6
6. 0.7
7. 0.7999999999999999

If you can't see what might be happening, do you know a way to cut that down to the decimal place so it's just 0.7 for example on the last one?

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You're seeing what happens when the floating-point representation of a value doesn't exactly match the decimal you want.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
gruntmonk

gruntmonk

    Newbie

  • Members
  • Pip
  • 5 posts
*deleted*

#8
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
All HTML code is considered a string by Javascript, so if you want to read the inner HTML, you will have to convert the string to a number. Try parseFloat().