09252001, 08:03 AM

#1

Registered User
Join Date: Sep 2001
Location: Milton Keynes, England
Posts: 2

Decimal places
I really need to have flash display calculations as currency (ie. two decimal places). A friend of mine has written me some code that looks to the decimal point and knocks off any characters after the next two, but this means that I don't get any answers like "0.80" or "2.00" because the zeroes won't appear!
Are there any math functions that cause two decimals to be displayed?



09252001, 09:42 AM

#2

Registered User
Join Date: Apr 2001
Location: Vancouver
Posts: 36

Code:
// Format a number to specified number of decimal places
// Written by Robert Penner in May 2001  www.robertpenner.com
// Optimized by Ben Glazer  [email protected]  on June 8, 2001
// Optimized by Robert Penner on June 15, 2001
Math.formatDecimals = function (num, digits) {
// If no decimal places needed, just use builtin Math.round
if (digits <= 0)
return String(Math.round(num));
//temporarily make number positive, for efficiency
if (num < 0) {
var isNegative = true;
num *= 1;
}
// Round the number to specified decimal places
// e.g. 12.3456 to 3 digits (12.346) > mult. by 1000, round, div. by 1000
var tenToPower = Math.pow(10, digits);
var cropped = String(Math.round(num * tenToPower));
// Prepend zeros as appropriate for numbers between 0 and 1
if (num < 1) {
while (cropped.length < digits+1)
cropped = "0" + cropped;
}
//restore negative sign if necessary
if (isNegative) cropped = "" + cropped;
// Insert decimal point in appropriate place (this has the same effect
// as dividing by tenToPower, but preserves trailing zeros)
var roundedNumStr = cropped.slice(0, digits) + "." + cropped.slice(digits);
return roundedNumStr;
}



09252001, 10:45 AM

#3

Registered User
Join Date: Sep 2001
Location: Milton Keynes, England
Posts: 2

Decimal Points
Thanks, Robert.
That looks like the business. In the meantime, however, I have discovered a completely bandit way of achieving what I need and I have implemented that.
In the meantime, thank you very much for your fast response! I will keep the code (if you don't mind) so I can implement it from the start rather than try to cobble it into a virtually finished project!
Meerkat



04122002, 10:21 AM

#4

Flash Padawan
Join Date: Oct 2001
Location: Manchester, England
Posts: 27

Implementation
I am relatively new to Math functions in Flash so forgive me if this is a simple question: How do you implement the above function as I want the answer to appear after a button click. I dont understand why there is Math.formatDecimals at the top as the dot notatation doesnt seem to make any sense.
Yours unknowingly
Matt



04172002, 10:01 PM

#5

Registered User
Join Date: Apr 2001
Location: Vancouver
Posts: 36

The code above defines a function called Math.formatDecimals(). You pass in two numbers, it spits out a string.



05092002, 04:36 AM

#6

Registered User
Join Date: May 2002
Posts: 7

Is there a reason why this code won't work(for me at least) in FlashMX?
It worked under flash 5, but now that I've upgraded, it outputs strange results(like 227 when the number is 0).
??



08022006, 03:22 PM

#7

Registered User
Join Date: Aug 2006
Posts: 1

what about if i dont want to have decimal point?
what the script will be?
means if the total must not have any decimal point but if the
aswer is 2.5 then it the answer will appere is 3.
how to do that?



08052006, 04:09 AM

#8

Lost In The Syntax
Join Date: Aug 2005
Location: Caledon, Ontario, Canada
Posts: 112

rounding to whole numbers
Quote:
Originally Posted by loveless
what about if i dont want to have decimal point?
what the script will be?
means if the total must not have any decimal point but if the
aswer is 2.5 then it the answer will appere is 3.
how to do that?

if you want to round a decimal place simply use:
Math.round() this will round 5,6,7,8,9 up and round 4,3,2,1 down,
Math.floor() rounds down
Math.ceil() rounds up
int() rounds to the number closer to 0



08292006, 11:39 PM

#9

Registered User
Join Date: Aug 2006
Posts: 1

Problems Implementing
Hi, I hope someone could help me with this, but I seem to be a complete moron about this. And I havent ever really used functions before.
Nevertheless, here was my attempt:
1.) I pasted the code into frame 1 on Actions layer: "Math.formatDecimals = function (num, digits) { ..."
2.) I call the function from a button with: "cart = Math.formatDecimals (tax, 2);"
Basically I am trying to get the string back into the variable cart.
It would be most helpful if I could get a sample file on this.?
anyone?



Thread Tools 

Display Modes 
Rate This Thread 
Linear Mode


Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off



All times are GMT. The time now is 12:12 PM.
///

