Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-13-2006, 09:43 PM   #1
doh
Registered User
 
Join Date: Feb 2006
Location: UK
Posts: 3
Question Adding a zero after decimal point in totals?

Hi, new here so sorry if this is a very basic question.

I'm making a money-bank game where coins get dragged across to the piggy-bank and the total is displayed throughout.

My problem is that, if say the amount is 10p, it is displayed as 0.1 rather than 0.10, or 1.1 for £1.10 etc. It would be great if I could get something to recognise those situations - adding 0 to make 0.10 - but not, say, to make 11p into 0.110.

I put in some seriously clunky code which checked if the amount was a specific number (such as 0.1, 0.2, 0.3 etc) and told it to add a 0 on, but I'm sure there must be a simpler way of doing this? Specifically to maybe try and get it to only scan the number AFTER the decimal point and if there's only 1 number there, then add a 0 on afterwards to display the correct amount.

I have a vague feeling it might have something to do with charAt(), but I'm not sure - it's not something I've looked at before. So any suggestions/help would be really appreciated.
doh is offline   Reply With Quote
Old 02-13-2006, 10:03 PM   #2
oka_
bleh.
 
oka_'s Avatar
 
Join Date: Jun 2004
Location: melbourne.aust
Posts: 303
Default

Hey there, you're on the right track for sure. I'd convert the number to a string, and test how many characters there are after the decimal point.

Something like...
ActionScript Code:
number = 1.5; foo = String(number); if (foo.substring(foo.indexOf(".", 0) + 1, foo.length).length == 1) {     foo += "0"; }

-oka
oka_ is offline   Reply With Quote
Old 02-14-2006, 08:51 AM   #3
doh
Registered User
 
Join Date: Feb 2006
Location: UK
Posts: 3
Thumbs up

That's great, thanks. It works brilliantly with all the numbers with decimal points in them - only it doesn't seem to make any distinctions between whole numbers and decimals. It displays £20 for £2, £10 for £1, and so on.

Any ideas on how to get around that? Thanks for the help so far.

Last edited by doh; 02-14-2006 at 09:12 AM.
doh is offline   Reply With Quote
Old 02-14-2006, 10:37 AM   #4
doh
Registered User
 
Join Date: Feb 2006
Location: UK
Posts: 3
Default

OK, I've sorted something out - not entirely sure how it works, but it does (at least up to £99.00, anyhow.) Thought I'd put it up in case anyone else has a similar problem and needs a simple solution - some of the currency solutions I've seen are pretty complicated to me..!!

Code:
Number = myNumbr;
foo = String (Number);
if (foo.substring(foo.indexOf("."< 0) + 0, foo.length).length == 1)
{    
displaytxt.text = " " + myNumbr + ".00";
}
else if (foo.substring(foo.indexOf("."< 0) + 0, foo.length).length == 2)
{    
displaytxt.text = " " + myNumbr + ".00";
}

else if (foo.substring(foo.indexOf(".", 0) + 1, foo.length).length == 1)
{    
displaytxt.text = " " + myNumbr + "0";
}
If anyone can think of anything simpler/works over 100, let me know. All this is a bit beyond what I'm used to doing....
doh 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
decimal point johnkainn ActionScript 2.0 6 10-10-2005 04:38 PM
adding point values to objects, and then summing them up bettypluto ActionScript 1.0 (and below) 0 05-11-2005 07:07 PM
input box comma to decimal point change help! spikeuk22 ActionScript 1.0 (and below) 3 10-27-2004 11:20 AM
adding decimal variables gives trouble Morg ActionScript 2.0 2 08-11-2004 08:39 AM
Need help reading a variable (with a decimal point) from a text file wzrd ActionScript 1.0 (and below) 3 10-16-2001 02:42 PM


All times are GMT. The time now is 01:45 PM.

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