A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Rounding numbers like money.

  1. #1
    Junior Member
    Join Date
    Sep 2015
    Posts
    6

    Rounding numbers like money.

    Hey,

    I've been trying to make a game where money is involved, but I'm stuck.
    I want to be able to show 1.00 as 1.00, and not as 1. I've done some googling, but I only get codes that would round 1.56937 to 1.57 or even to 1.56, but not in the way that I want it.
    Can someone help me out?

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    but I only get codes that would round 1.56937 to 1.57 or even to 1.56, but not in the way that I want it.
    Hi,

    In what way would you want the numbers to go to?

    What would you want 1.56937 to end up as.

    PHP Code:
    var input:Number 1.56937;

    var 
    output1:Number Math.ceil(input 100) / 100;// goes high
    var output2:Number Math.round(input 100) / 100;// nearest higher or lower
    var output3:Number Math.floor(input 100) / 100;// goes low

    trace(output1);
    trace(output2);
    trace(output3); 
    if you mean to make it look like money, try here http://stackoverflow.com/questions/8...ormat-a-number
    Last edited by fruitbeard; 09-07-2015 at 12:05 PM.

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Here is something better I just found, mess around with it.

    PHP Code:
    var input:Number 12345678.99;

    function 
    formatNumbers(num:Numbercomma:Boolean):String
    {
        
    // return a zero value if num is not valid  
        
    if (isNaN(num))
        {
            return 
    "0.00";
        }
        
    // round num to the nearest 100th       
        
    num Math.round(num 100) / 100;
        
    // convert num to a string  
        
    var num_str:String String(num);
        
    // seperate any decimals from the whole numbers  
        
    var num_array num_str.split(".");
        
    // if there are no decimals add them using "00"  
        
    if (num_array[1] == undefined)
        {
            
    num_array[1] = "00";
        }
        
    // if the decimals are too short, add an extra "0"       
        
    if (num_array[1].length == 1)
        {
            
    num_array[1] += "0";
        }
        
    // separate whole numbers with commas      
        // if required (comma = true)  
        
    if (comma)
        {
            var 
    whole_array:Array = new Array();
            var 
    start:Number;
            var 
    end:Number num_array[0].length;
            while (
    end 0)
            {
                
    start Math.max(end 30);
                
    whole_array.unshift(num_array[0].slice(startend));
                
    end start;
            }
            
    num_array[0] = whole_array.join(",");
        }
        
    // construct a return string joining      
        // the whole numbers with the decimals  
        
    return (num_array.join("."));
    }
    trace(formatNumbers(input));
    trace(formatNumbers(inputfalse));
    trace(formatNumbers(inputtrue)); 

  4. #4
    Junior Member
    Join Date
    Sep 2015
    Posts
    6
    Thanks man, I'll look into it

  5. #5
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    http://snipplr.com/view/27081/ This looks to be a simplified function to take care of it. If you care to test it.
    .

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    That link is for AS3 swak and it outputs some extra commas too, like the SO link in post No. 2 does, it also uses shorthand coding which I think is a little confusing, especially for a starter.

    The other one is simple too if you remove the comments ( not my code, found on net ) but works great.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center