0

I have a set of strings in my dom with the same class. I need to convert the strings to numbers and then perform a cacluation on them and return them to the screen. I have used parseInt, parseFloat, Number() and even coercion using +variable; without any converson. I show typeof as "string" in every case. I am unsure what I am doing wrong. Can someone please help me find what I am missing? Here s my html:

<div class="price">
  <span class="label">from</span>
  <span class="value">
    <span class="text-lg lh1em item "> $3,845.00</span>
  </span>
  <span class="value">
    <span class="text-lg lh1em item "> $3,645.00</span>
  </span>
</div>

and my Javascript is as follows:

let customPrice = document.getElementsByClassName('lh1em');

Array.from(customPrice).forEach(function(dollarAmount) {
    let withoutDollar = dollarAmount.innerText.substr(1); 
    let withoutComa = withoutDollar.replace(",",'');
    // parseFloat(withoutComa);
    let noPointZero = withoutComa.replace(/\.00/, '');
     // noPointZero * 1;
    // parseInt(noPointZero);
    Number(noPointZero);
    console.log(typeof noPointZero);
}); 

Without the typeof, I get the correct number value as a string. How can I force this to be a number? Thank you.

Erik James Robles
  • 721
  • 2
  • 12
  • 24
  • 1
    Numbers and strings are immutable. Assign the conversion to a variable to be used, `noPointZero = Number(noPointZero);` – evolutionxbox Feb 25 '21 at 00:25
  • @evolutionbox was right. I needed to redeclare my variable with the number() function and it is now returning number as the typeof. – Erik James Robles Feb 25 '21 at 00:29
  • Does this answer your question? [How to convert a string to an integer in JavaScript?](https://stackoverflow.com/questions/1133770/how-to-convert-a-string-to-an-integer-in-javascript) – Heretic Monkey Feb 25 '21 at 00:36

3 Answers3

2

You have a space at the beginning of the text. So substr(1) is removing the space, not the $. Use trim() to remove surrounding whitespace.

You also need to assign the result of Number() at the end. It's just like all your other fixups -- it returns a new value, it doesn't modify the value in place.

To replace commas you need to use a regular expression with the g modifier. Replacing a string only replaces the first occurrence.

Array.from(customPrice).forEach(function(dollarAmount) {
    let withoutDollar = dollarAmount.innerText.trim().replace(/^\$/, '');
    let withoutComa = withoutDollar.replace(/,/g,'');
    // parseFloat(withoutComa);
    let noPointZero = withoutComa.replace(/\.00$/, '');
     // noPointZero * 1;
    // parseInt(noPointZero);
    let numeric = Number(noPointZero);
    console.log(typeof numeric);
}); 
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Numbers and strings are immutable. Assign the conversion to a variable to be used later.

let customPrice = document.getElementsByClassName('lh1em');

Array.from(customPrice).forEach(function(dollarAmount) {
    let withoutDollar = dollarAmount.innerText.substr(1); 
    let withoutComa = withoutDollar.replace(",",'');
    let noPointZero = withoutComa.replace(/\.00/, '');
    noPointZero = Number(noPointZero);
    console.log(typeof noPointZero, noPointZero);
}); 
<div class="price">
  <span class="label">from</span>
  <span class="value">
    <span class="text-lg lh1em item "> $3,845.00</span>
  </span>
  <span class="value">
    <span class="text-lg lh1em item "> $3,645.00</span>
  </span>
</div>
evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
0

There's a space your substr() is getting rid of. Do .replace(/[^0-9.]/g, '') instead.

Hashbrown
  • 12,091
  • 8
  • 72
  • 95