1

Please don't barate me for asking an "dumb" question but I've been struggling over this for weeks!

Why does this array leave off my leading zeros and convert the numbers to a different format even after I've used toString() to convert the values to a string?

var coords=new Array();
coords[0]=new Array(03336890,"SPOON RIVER NEAR ST. JOSEPH, IL");
coords[1]=new Array(03346500,"EMBARRAS RIVER AT LAWRENCEVILLE, IL");
coords[2]=new Array(03612600,"OHIO RIVER AT OLMSTED");
coords[3]=new Array(05543010,"ILLINOIS RIVER AT SENECA, IL");
coords[4]=new Array(05554300,"INDIAN CREEK NEAR FAIRBURY, IL");
coords[5]=new Array(05576100,"LICK CREEK NEAR WOODSIDE, IL");
coords[6]=new Array(05576195,"SUGAR CREEK NEAR CHATHAM, IL");
coords[7]=new Array(05586300,"ILLINOIS RIVER AT FLORENCE, IL");
coords[8]=new Array(05599490,"BIG MUDDY RIVER AT RTE 127 AT MURPHYSBORO, IL");
coords[9]=new Array(411955088280601,"HANSON GRAVEL PIT AT CULVERT NEAR MORRIS, IL");

for (i=0;i<coords.length;i++){
    coords[i][0].toString();
}

Check out the result on jsfiddle

2 Answers 2

2

Set the numbers in a quote instead of numbers.

var coords=new Array();
coords[0]=new Array("03336890","SPOON RIVER NEAR ST. JOSEPH, IL");
coords[1]=new Array("03346500","EMBARRAS RIVER AT LAWRENCEVILLE, IL");
...

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

That was so easy it was hard! Thanks for pointing out the obvious! I really needed it!
Appreciated. Thank you.
0

The problem here is that you saved the values as a number. javascript automatically saves them internal without the leading zeros, so they get lost before you even call .toString() on them. I made you a workaround with your given example witch uses a little trick to give you back the leading zero. Of course this needs some refinement if needed:

var i, x = 100

var coords = new Array();
coords[0] = new Array(03336890, "SPOON RIVER NEAR ST. JOSEPH, IL");
coords[1] = new Array(03346500, "EMBARRAS RIVER AT LAWRENCEVILLE, IL");
coords[2] = new Array(03612600, "OHIO RIVER AT OLMSTED");
coords[3] = new Array(05543010, "ILLINOIS RIVER AT SENECA, IL");
coords[4] = new Array(05554300, "INDIAN CREEK NEAR FAIRBURY, IL");
coords[5] = new Array(05576100, "LICK CREEK NEAR WOODSIDE, IL");
coords[6] = new Array(05576195, "SUGAR CREEK NEAR CHATHAM, IL");
coords[7] = new Array(05586300, "ILLINOIS RIVER AT FLORENCE, IL");
coords[8] = new Array(05599490, "BIG MUDDY RIVER AT RTE 127 AT MURPHYSBORO, IL");
coords[9] = new Array(411955088280601, "HANSON GRAVEL PIT AT CULVERT NEAR MORRIS, IL");

for (i = 0; i < 10; i++) {
  if (x < 10000000) {
    alert("0" + coords[i]);
  }
}

Link to the jsfiddle is here: https://jsfiddle.net/gyjjqnkt/

1 Comment

This doesn't really resolve the problem of the numbers being converted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.