-
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?
-
.
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.
-
.
Hi,
Here is something better I just found, mess around with it.
PHP Code:
var input:Number = 12345678.99;
function formatNumbers(num:Number, comma: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 - 3, 0); whole_array.unshift(num_array[0].slice(start, end)); 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(input, false)); trace(formatNumbers(input, true));
-
Thanks man, I'll look into it
-
http://snipplr.com/view/27081/ This looks to be a simplified function to take care of it. If you care to test it.
-
.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|