Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    LXS
    LXS is offline
    New to the CF scene
    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));
    The complete code is at the end, I want to mention that the code below in comment (if...if...if) it works great but is very bad to read and to edit something, so I try to use a loop.
    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=i-3)
    	{
    		/*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>=1e-24&&value<10e-22)
    	{
    		rval=value*1e+24;
    		sufix="yocto";
    	}
    	if(value>=1e-21&&value<10e-19)
    	{
    		rval=value*1e+21;
    		sufix="zepto";
    	}
    	if(value>=1e-18&&value<10e-16)
    	{
    		rval=value*1e+18;
    		sufix="atto";
    	}
    	if(value>=1e-15&&value<10e-13)
    	{
    		rval=value*1e+15;
    		sufix="femto";
    	}
    	if(value>=1e-12&&value<10e-10)
    	{
    		rval=value*1e+12;
    		sufix="pico";
    	}
    	if(value>=1e-9&&value<10e-7)
    	{
    		rval=value*1e+9;
    		sufix="nano";
    	}
    	if(value>=1e-6&&value<10e-4)
    	{
    		rval=value*1e+6;
    		sufix="micro";
    	}
    	if(value>=1e-3&&value<10e-1)
    	{
    		rval=value*1e+3;
    		sufix="mili";
    	}
    	if(value>=1e-2&&value<10e-1)
    	{
    		rval=value*1e+2;
    		sufix="centi";
    	}
    	if(value>=1e-1&&value<10e-1)
    	{
    		rval=value*1e+1;
    		sufix="deci";
    	}
    	if(value>=1e+1&&value<10e+1)
    	{
    		rval=value*1e-1;
    		sufix="deca";
    	}
    	if(value>=1e+2&&value<10e+2)
    	{
    		rval=value*1e-2;
    		sufix="hecto";
    	}
    	if(value>=1e+3&&value<10e+5)
    	{
    		rval=value*1e-3;
    		sufix="kilo";
    	}
    	if(value>=1e+6&&value<10e+8)
    	{
    		rval=value*1e-6;
    		sufix="mega";
    	}
    	if(value>=1e+9&&value<10e+11)
    	{
    		rval=value*1e-9;
    		sufix="giga";
    	}
    	if(value>=1e+12&&value<10e+14)
    	{
    		rval=value*1e-12;
    		sufix="tera";
    	}
    	if(value>=1e+15&&value<10e+17)
    	{
    		rval=value*1e-15;
    		sufix="peta";
    	}
    	if(value>=1e+18&&value<10e+20)
    	{
    		rval=value*1e-18;
    		sufix="exa";
    	}
    	if(value>=1e+21&&value<10e+23)
    	{
    		rval=value*1e-21;
    		sufix="zetta";
    	}
    	if(value>=1e+24&&value<10e+26)
    	{
    		rval=value*1e-24;
    		sufix="yotta";
    	}*/
    	return [rval,sufix];
    }
    Last edited by LXS; 12-19-2012 at 02:52 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 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 (12-19-2012)

  • #3
    LXS
    LXS is offline
    New to the CF scene
    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 fast-reply and for explanations!

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 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 (12-19-2012)

  • #5
    LXS
    LXS is offline
    New to the CF scene
    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 />");

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 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 (12-20-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •