How can such a line: 12 34 , 123 456 converted into a number?

 var counter = 2; num = $('i').text(); console.log(num); console.log(parseInt(num)); console.log(num*counter); 
 <script src="https://code.jquery.com/jquery-2.0.3.js"></script> <span><i>120 250</i> коп.</span> 

  • one
    remove space before conversion - Grundy
  • one
    num = num.replace(/\s+/g, ""); - user194374
  • @kff, and like this parseInt(num.replace(/\s+/g, ''),10) . thanks decided - HamSter

2 answers 2

So decided by parseInt(num.replace(/\s+/g, ''),10) :

 var num = $('i').text(); console.log(num); console.log(parseInt(num.replace(/\s+/g, ''),10)); 
 <script src="https://code.jquery.com/jquery-2.0.3.js"></script> <span><i>120 250</i> коп.</span> 

thanks everyone!

  • what's the counter? - Grundy
  • @Grundy, yes, I copied it from my own example, I still have to multiply it by num - HamSter
  • Well, you at least write exactly how you decided, and not just a piece of code - Grundy
 parseInt(String(num).replace(/ /g, '')) 

UPD

String(num) needed to avoid errors

replace is not a function

in case num was just a number. For example, if this code is used in a function, and once on its input the tax is not '12', but 12.

  • 3
    What is the meaning of String (num) ? - Grundy
  • @Grundy, I apologize for a separate answer. Does not allow to make comments. String(num) to avoid the error replace is not a function if num was just a number. For example, if this code is used in a function, and once at its input the tax is not '12', but 12. - Sasha
  • @ Sasha register - otherwise it’s not okay for you to comment on your own posts - PashaPash