Results 1 to 6 of 6

Thread: Format of the display numbers

  1. #1
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Format of the display numbers

    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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Scriptencrypter (04-13-2012)

  4. #3
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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?

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    4.6788256E+15
    Do:

    Code:
    var num = 4.6788256E+15;
    alert(num);
    We get:

    4678825600000000
    Now do:

    Code:
    var num = 4.6788256E+15;
    alert(num *= 1000000);
    We get:

    4.6788256e+21
    Now do:

    Code:
    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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Scriptencrypter (04-15-2012)

  7. #5
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

  8. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

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

    Code:
    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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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