• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

Trying to convert 0025 into an integer.. Pls help!

I'm grabbing a value from a database that returns numbers like 0025 or 0042. It really means 25 and 42. How do I put thos values into a javascript variable while letting it know that its an integer? Javascript doesn't like numbers with double zeros in front of it.  It returning 0025 as 21 for some reason. How do I convert 0025 to 25?
0
bemara57
Asked:
bemara57
1 Solution
 
Pravin AsarPrincipal Systems EngineerCommented:
alert (parseInt (0025));
alert (parseInt ('0042'));
0
 
ThijsSCommented:
If there are some numbers with decimals, use parseFloat(NumberFromDatabase). If there aren't any decimal numbers, just integers, use: parseInt(NumberFromDatabase) as in the comment above.
0
 
ZvonkoSystems architectCommented:
You have to put the radix as second parameter of the parseInt() method.
Like this:

var theNum = parseInt("0025", 10);

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ZvonkoSystems architectCommented:
Oh, the default radix for numbers starting with zero is eight (octal numbers).

0
 
Pravin AsarPrincipal Systems EngineerCommented:
Use this one...

function removeLeadZero (z) {
while (z.charAt(0)=="0")
{
z=z.substring(1,z.length);
}

// Case all 0
if (!z.length) { z="0"; }
return z;
}
alert (parseInt(removeLeadZero('0025')));
alert (parseInt(removeLeadZero('0042')));
alert (parseInt(removeLeadZero('000000')));
0
 
bemara57Author Commented:
AH HAH!!! Zvonko, thanks again. That was the problem
0
 
ZvonkoSystems architectCommented:
You are welcome.

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now