Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6
Thread: JavaScript > Long number sufix

12192012, 02:50 PM #1
 Join Date
 Dec 2012
 Posts
 3
 Thanks
 3
 Thanked 0 Times in 0 Posts
JavaScript > Long number sufix
Hi, I'm new here on this forum and I have registered to receive help in certain situations and help others members if I can.
I start with a problem which gives me a headache, I created a prototype function that adds a multiple or submultiple metric string, works in if..if..if case but if I try to compact the code in a shorten way I get an error .
In this case 1e+5 => 100,000, with an alert: alert (1e+5) works perfectly, but when I try this code does not work at all:
Code:var pw = 5; alert (1e + (pw));
If anyone has a better solution please post it.
Thank you very much, I owe you one!
Code:var ESE = {}; ESE.Component = function() { /* ... ... ... */ } ESE.prototype.getPosSufix=function(value) { var sufix=""; var rval=value; var i,j=0; var sTable= [ /*Metric multiples*/ "yotta","zetta","exa","peta","tera", "giga","mega","kilo","hecto","deca", /*Metric submultiples*/ "deci","centi","mili","micro","nano", "pico","femto","atto","zepto","yocto" ]; for(i=24;i>=1;i=i3) { /*if(value>=(1e+(i))&&value<(10e+(i+2))) { if(i==3 && j==8) {i+=3} rval=value*(1e(i)); sufix=sTable[j]; }*/ alert(1e+0+i); j++; } /*if(value>=1e24&&value<10e22) { rval=value*1e+24; sufix="yocto"; } if(value>=1e21&&value<10e19) { rval=value*1e+21; sufix="zepto"; } if(value>=1e18&&value<10e16) { rval=value*1e+18; sufix="atto"; } if(value>=1e15&&value<10e13) { rval=value*1e+15; sufix="femto"; } if(value>=1e12&&value<10e10) { rval=value*1e+12; sufix="pico"; } if(value>=1e9&&value<10e7) { rval=value*1e+9; sufix="nano"; } if(value>=1e6&&value<10e4) { rval=value*1e+6; sufix="micro"; } if(value>=1e3&&value<10e1) { rval=value*1e+3; sufix="mili"; } if(value>=1e2&&value<10e1) { rval=value*1e+2; sufix="centi"; } if(value>=1e1&&value<10e1) { rval=value*1e+1; sufix="deci"; } if(value>=1e+1&&value<10e+1) { rval=value*1e1; sufix="deca"; } if(value>=1e+2&&value<10e+2) { rval=value*1e2; sufix="hecto"; } if(value>=1e+3&&value<10e+5) { rval=value*1e3; sufix="kilo"; } if(value>=1e+6&&value<10e+8) { rval=value*1e6; sufix="mega"; } if(value>=1e+9&&value<10e+11) { rval=value*1e9; sufix="giga"; } if(value>=1e+12&&value<10e+14) { rval=value*1e12; sufix="tera"; } if(value>=1e+15&&value<10e+17) { rval=value*1e15; sufix="peta"; } if(value>=1e+18&&value<10e+20) { rval=value*1e18; sufix="exa"; } if(value>=1e+21&&value<10e+23) { rval=value*1e21; sufix="zetta"; } if(value>=1e+24&&value<10e+26) { rval=value*1e24; sufix="yotta"; }*/ return [rval,sufix]; }
Last edited by LXS; 12192012 at 02:52 PM.
12192012, 03:06 PM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,492
 Thanks
 205
 Thanked 2,579 Times in 2,557 Posts
Try this:
Code:<script type = "text/javascript"> var pw = "5"; // a string value, not a number var x = "1e" + pw; // concatenate strings x = Number(x); // make a number alert (x); // 100000 x = x.toExponential(); alert (x); // 1e+5 </script>
Quizmaster: A monocracy is a form of government in which how many people rule?
Contestant: Twelve.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Users who have thanked Philip M for this post:
LXS (12192012)
12192012, 03:33 PM
#3
 Join Date
 Dec 2012
 Posts
 3
 Thanks
 3
 Thanked 0 Times in 0 Posts
Number object do the trick, I did not thought about this solution (to convert to a number object), because the type of 1e+24 is treated as a number, but simply 1e does not mean anything.
Thank you for fastreply and for explanations!
12192012, 09:45 PM
#4
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 6,923
 Thanks
 0
 Thanked 684 Times in 673 Posts
Another alternative would be to use parseFloat.
Code:var pw = 5; alert (parseFloat('1e' + (pw)));
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.
Users who have thanked felgall for this post:
LXS (12192012)
12192012, 10:32 PM
#5
 Join Date
 Dec 2012
 Posts
 3
 Thanks
 3
 Thanked 0 Times in 0 Posts
Again thanks a lot for your help!
There is the final result:
Code:function getPrefix(value) { var prefix=""; var retVal = value; var i, j=0; var min, max, mul; // Table ordered by smallest submultiple to highest multiple var sTable= [ // Metric submultiples "yocto","zepto","atto","femto","pico", "nano","micro","mili","centi","deci", // Metric multiples "deca","hecto","kilo","mega","giga", "tera","peta","exa","zetta","yotta" ]; for(i=24; i<=24; i+=3) { min="1e"+(i.toString()); min=Number(min); max="1e"+((i+3).toString()); max=Number(max); if(i==0) i+=1; if(i>=0) mul="1e"+(Math.abs(i).toString()); else mul="1e"+(Math.abs(i).toString()); mul=Number(mul); if(value >= min && value < max) { prefix = sTable[j]; retVal = value * mul; document.write("_____PICKED_____<br />"); document.write("[I: "+i+"]<br />X: "+min+" > Y: "+max+" > Z: "+mul+"<br />"); break; } else if(prefix == "" && i == 24) prefix = "prototype prefix"; document.write("[I: "+i+"]<br />X: "+min+" > Y: "+max+" > Z: "+mul+"<br />"); if(i >= 3 && i < 0  i > 0 && i < 3) i = 2; j++; } return [retVal,prefix]; } var gp=getPrefix(0.1); document.write("Value: " + gp[0] + " " + gp[1] + "<br />");
12202012, 07:35 AM
#6
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,492
 Thanks
 205
 Thanked 2,579 Times in 2,557 Posts
Great, but you should get rid of document.write() which is obsolete. Remember that any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it).
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Users who have thanked Philip M for this post:
LXS (12202012)