View Full Version : Format of the display numbers

Scriptencrypter

04-13-2012, 01:25 AM

Hi

I am writing a script where it will convert the characters typed in an input field to their respective ASC chacter codes. I have it getting out a number and then multiplying it by a designated number. When it outputs a number that is the code. Then here is the problem. It always gives the number in scientific notation.

4.6788256 E+15

I want the number to display as a standard number with all the digits. Is there a way to do this?

Thanks in Advance,

ScriptEncrypter

jscheuer1

04-13-2012, 03:00 AM

Not unless you multiply by a smaller number.

That number you have there in your post is small enough that most browsers would render it as:

4678825600000000

But if you get a lot larger than that you get an exponential value. I just tried, once I multiplied it by 1000000, the browser would no longer render it without an exponent.

As long as you don't have a space between the last digit in the main expression and the E, and as long as you don't do anything to it that converts it into a string, it's still a number though. Even if you convert it to a string, you can always convert it back to a number later.

Scriptencrypter

04-13-2012, 11:06 PM

Is there a way to possibly get that number as something other than a "number" to where it would read the "number" as a number in something other than base 10 like maybe in base 7?

jscheuer1

04-14-2012, 03:48 AM

I'm not really sure where you're going with this, but yes.

Let's take our original number (space removed as per my suggestion to make it be viewed as a number by the browser):

4.6788256E+15

Do:

var num = 4.6788256E+15;

alert(num);

We get:

4678825600000000

Now do:

var num = 4.6788256E+15;

alert(num *= 1000000);

We get:

4.6788256e+21

Now do:

var num = 4.6788256E+15;

num *= 1000000;

alert(num.toString(7));

We get:

33264540354344104223433301

which is a string representation of 4.6788256e+21's value in Base 7.

We can get back (actually, owing to the vagaries of javascript math and/or other factors and/or depending upon the number and perhaps the browser, very close to it) the base 10 equivalent by using parseInt:

var num = 4.6788256E+15;

num *= 1000000;

alert(num);

num = num.toString(7);

alert(parseInt(num, 7));

Which gives us:

4.6788256e+21

and then for the second alert (in Firefox at least):

4.6788256000000005e+21

Exactly the same except for the 0.0000000000000005e+21 added due to, well I'm not sure exactly what, perhaps because there's no exact equivalency, or to browser/javascript error - Surprise! javascript/browser math isn't always exact.

Scriptencrypter

04-15-2012, 01:53 PM

Thanks So much. This is exactly what I needed. I cant express my thanks. I will post more of I run into any other problems.

jscheuer1

04-15-2012, 04:11 PM

There will probably be problems in IE though. If we do:

var num = 4.6788256E+15;

num *= 1000000;

alert(num);

num = num.toString(7);

document.write(num + ' = ' + parseInt(num, 7));

IE alerts the same as Firefox:

4.6788256e+21

But when IE writes the base 7 number, it's different, and so therefore is the final result. Firefox writes:

33264540354344104223433301 = 4.6788256000000005e+21

IE:

3.3264540354344104226(e+25) = 3

The first part is technically accurate, as is the second given that string value we feed to parseInt. However, mathematically speaking, it's wrong and certainly not even close to the value we're looking for.

I thought, well we can split the string up and do math on it to get the correct answer. I tried that, but I see no way of doing it that comes close. You can take a parseFloat of 3.3264540354344104226 in base 7 and multiply that times Math.pow(7, 25), and that should be it. It is close, but still too far off:

var num = 4.6788256E+15;

num *= 1000000;

alert(num);

num = num.toString(7);

if(/e/i.test(num)){

document.write(num + ' = ');

num = num.replace(/[()]/g, '').split(/e/i);

num = parseFloat(num[0], 7) * Math.pow(7, eval(num[1]));

document.write(num);

} else {

document.write(num + ' = ' + parseInt(num, 7));

}

Which gives in IE:

3.3264540354344104226(e+25) = 4.4610031216756507e+21

I think I got the math right, so unless I missed something, I'm not sure what else can be done for IE.

Powered by vBulletin® Version 4.2.2 Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.