Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 09-25-2001, 08:03 AM   #1
Meerkat
Registered User
 
Join Date: Sep 2001
Location: Milton Keynes, England
Posts: 2
Unhappy 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?
Meerkat is offline   Reply With Quote
Old 09-25-2001, 09:42 AM   #2
robertpenner
Registered User
 
Join Date: Apr 2001
Location: Vancouver
Posts: 36
Default

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 built-in 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;
}
robertpenner is offline   Reply With Quote
Old 09-25-2001, 10:45 AM   #3
Meerkat
Registered User
 
Join Date: Sep 2001
Location: Milton Keynes, England
Posts: 2
Default 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
Meerkat is offline   Reply With Quote
Old 04-12-2002, 10:21 AM   #4
kerouac9
Flash Padawan
 
kerouac9's Avatar
 
Join Date: Oct 2001
Location: Manchester, England
Posts: 27
Question 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
kerouac9 is offline   Reply With Quote
Old 04-17-2002, 10:01 PM   #5
robertpenner
Registered User
 
Join Date: Apr 2001
Location: Vancouver
Posts: 36
Default

The code above defines a function called Math.formatDecimals(). You pass in two numbers, it spits out a string.
robertpenner is offline   Reply With Quote
Old 05-09-2002, 04:36 AM   #6
dizzy
Registered User
 
Join Date: May 2002
Posts: 7
Default

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

??
dizzy is offline   Reply With Quote
Old 08-02-2006, 03:22 PM   #7
loveless
Registered User
 
Join Date: Aug 2006
Posts: 1
Default

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?
loveless is offline   Reply With Quote
Old 08-05-2006, 04:09 AM   #8
Graphic
Lost In The Syntax
 
Graphic's Avatar
 
Join Date: Aug 2005
Location: Caledon, Ontario, Canada
Posts: 112
Send a message via MSN to Graphic Send a message via Skype™ to Graphic
Post 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
Graphic is offline   Reply With Quote
Old 08-29-2006, 11:39 PM   #9
keewong
Registered User
 
Join Date: Aug 2006
Posts: 1
Exclamation 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?
keewong is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Keeping answers to two decimal places randocommando ActionScript 2.0 15 11-04-2009 07:23 PM
decimal places.... flashmxkid ActionScript 1.0 (and below) 2 09-08-2004 02:30 PM
decimal places sandyas ActionScript 1.0 (and below) 3 02-14-2004 09:44 AM
decimal places rcr639 ActionScript 1.0 (and below) 4 05-26-2002 03:14 AM
Showing zeroes as decimal places kerouac9 ActionScript 1.0 (and below) 16 04-18-2002 12:26 PM


All times are GMT. The time now is 09:05 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.