Results 1 to 12 of 12

Thread: 1.50 instead of 1.5....how?

  1. #1

    1.50 instead of 1.5....how?

    while adding floats, how do i get the trailing 0? cause flash just seems to terminate it! and also is there a way to fix the number of digits after the decimal place?
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  2. #2
    Jephz's Avatar
    106
    posts
    The Penguin n00b
    aren't they the same thing?
    Don't you ever, ever, EVER call me a Noob. I mean it!

  3. #3
    Quote Originally Posted by Jephz View Post
    aren't they the same thing?
    They are.

  4. #4
    Do you know how floating point math works?
    Floating Point

    Are you asking about representing a float as a string with x number of decimal digit
    toFixed()

    Also look at toPrecision in case you need it later.
    Last edited by Sirisian; May 15th, 2008 at 02:22 AM.

  5. #5
    I didnt find those in the help manual sirisian...i am using AS2, can you help in some more detail.

    And you all are right, 1.5 is same as 1.50 in math...but you see i am setting up the score system for "money" and in monetary system 1.5(five cent) and 1.50(fifty cent) are different. And this is also the reason i need 2 fixed decimal places in the number.

    if i do 1.50+1.50, it will give 3, but i want 3.00.....do you guys see my problem now?
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  6. Quote Originally Posted by bluemagica View Post
    I didnt find those in the help manual sirisian...i am using AS2, can you help in some more detail.

    And you all are right, 1.5 is same as 1.50 in math...but you see i am setting up the score system for "money" and in monetary system 1.5(five cent) and 1.50(fifty cent) are different. And this is also the reason i need 2 fixed decimal places in the number.

    if i do 1.50+1.50, it will give 3, but i want 3.00.....do you guys see my problem now?
    If you can't find a better way, couldn't you just check for the length and add a zero if necessary?

  7. #7
    That is a make shift way, i am looking for some actual way out!
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  8. #8
    Hmm, I thought I told everyone to use AS3 a while back. I guess the word didn't get around. AS2 has no native method for turning a Number into a fixed type.

    var n1:Number = 1.5;
    var n2:Number = 1.5;
    var n3:Number = n1 + n2;
    var n1Plusn2:String = (n3 * 100).toString();
    var indexOfPeriod:int = n1Plusn2.indexOf(".");
    n1Plusn2 = n1Plusn2.substring(0, indexOfPeriod - 1) + "." + n1Plusn2.substr(indexOfPeriod, 2);

    something like that. You might have to test it.

    Here's the documentation for string.

    Also this isn't really game programming related. Remember to say the language you are using or people might assume it's AS3.

  9. #9
    cool code sirisian but i would say its safe to say that the chancs of it being as2 is still pretty high so dont assume nothing

  10. #10
    Maybe a bit late, but these two functions might be useful:
    Code:
    // turn a number into a currency string 
    function toCurr(num:Number):String {
    	var currency:String = "";
    	var majUnit:Number = Math.floor(num);
    	var minUnit:Number = Math.round((num - majUnit) * 100);
    	var majUnitStr:String = String(majUnit);
    	var minUnitStr:String;
    	minUnit < 10 ? minUnitStr = "0" + String(minUnit) : minUnitStr = String(minUnit);
    	return currency + majUnit + "." + minUnitStr;
    }
    // turn a currency string into a number 
    function fromCurr(currency:String):Number {
    	var str:String = currency.substr(1);
    	return parseFloat(str);
    }
    //testing - note the subtle difference if you want to perform any maths functions on currency strings
    var a:Number = 1.50;
    trace("The sum of two numbers is " + (a + a));
    trace("The sum of two currency amounts is " + toCurr(a + a));
    var b:String = "1.50";
    trace("The sum(!) of two currency strings is " + (b + b));
    trace("The sum of two currency amounts is " + (fromCurr(b) + fromCurr(b)));
    2006-11 GlosRFC - Searching 8,168,684,336 brain cells

  11. #11
    I would think the easiest way is to have a number that represents currency, and just use cents, don't bother with dollars/pounds. So to add 1 you actually add 100. Then for the display use a custom toString method that uses the length of the number, and if it is less than three add "0." to the start, and if it is greater than three change it to "" + (all the digits except the last two) + "." + (the last two digits).

  12. #12
    umm guys, thanks for all the help, but i have managed to solve it using a makeshift way! But Still those were really a great help in my learning
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012