Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2012, 02:50 PM   PM User | #1
LXS
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
LXS is an unknown quantity at this point
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..
LXS is offline   Reply With Quote
Old 12-19-2012, 03:06 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
LXS (12-19-2012)
Old 12-19-2012, 03:33 PM   PM User | #3
LXS
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
LXS is an unknown quantity at this point
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!
LXS is offline   Reply With Quote
Old 12-19-2012, 09:45 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
LXS (12-19-2012)
Old 12-19-2012, 10:32 PM   PM User | #5
LXS
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
LXS is an unknown quantity at this point
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 />");
LXS is offline   Reply With Quote
Old 12-20-2012, 07:35 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
LXS (12-20-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:45 AM.


Advertisement
Log in to turn off these ads.