Like .substr () integer in Javascript

As the name says, what function will give me a result similar to what .substr () does, only for integers?

Thanks!

UPDATE:

Here's what doesn't work:

if ($(#itemname).val() == "Not Listed") { var randVal = Math.random() * 10238946; var newVal = randVal.toString().substr(0, 4); $("#js_itemid").val(randVal); $("#js_price").val("199.99"); } 
+6
source share
5 answers

What about...

 var integer = 1234567; var subStr = integer.toString().substr(0, 1); 

...?

+26
source

Considering

 var a = 234; 

There are several methods for converting a number to a string to extract a substring:

  • string concatenation
  • Method Number.prototype.toString ()
  • pattern strings
  • String object

Examples

Included are examples of how a given number, a , can be converted / powered.

Concatenation of an empty string

 (a+'').substr(1,1); // "3" 

Number.prototype.toString Method

 a.toString().substr(1,1) // "3" 

Pattern strings

 '${a}'.substr(1,1) // "3" 

String object

 String(a).substr(1,1) // "3" 
+3
source

Can conversion to string be normal at first?

 var x = 12345; var xSub = x.toString().substr(1,3); alert(xSub);​ // alerts "234" 
+1
source

You must first convert it to a string with toString()

 var a = 105; alert(a.toString().substr(1,2)); 
0
source

You can try this:

 <script> var x = '146870'; function format(num){ return (num / 100).toFixed(2); } alert(format(x)); </script> 
0
source

All Articles