Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums Decimal places
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 09-25-2001, 09:03 AM #1 Meerkat 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?
 09-25-2001, 10:42 AM #2 robertpenner 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 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; }```
 09-25-2001, 11:45 AM #3 Meerkat 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
 04-12-2002, 11:21 AM #4 kerouac9 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
 04-17-2002, 11:01 PM #5 robertpenner 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.
 05-09-2002, 05:36 AM #6 dizzy 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). ??
 08-02-2006, 04:22 PM #7 loveless 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?
08-05-2006, 05:09 AM   #8
Graphic
Lost In The Syntax

Join Date: Aug 2005
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

 08-30-2006, 12:39 AM #9 keewong 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 Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

 Similar Threads Thread Thread Starter Forum Replies Last Post randocommando ActionScript 2.0 15 11-04-2009 08:23 PM flashmxkid ActionScript 1.0 (and below) 2 09-08-2004 03:30 PM sandyas ActionScript 1.0 (and below) 3 02-14-2004 10:44 AM rcr639 ActionScript 1.0 (and below) 4 05-26-2002 04:14 AM kerouac9 ActionScript 1.0 (and below) 16 04-18-2002 01:26 PM

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

///