PDA

View Full Version : Help with Decimals PLEASE!


akschinas
06-28-2004, 07:57 PM
:mad: Hi. I combed the forum to answer my question about allowing 2 decimal places for currency. Nothing was working until I came across this code:

if (Math.floor(x*100)%100 == 0) {
x += ".00";
} else if (Math.floor(x*100)%10 == 0) {
x += "0";
}

I'm adding approx 20 items in a column together and although very messy, it makes the decimal points come up. Here's my problem... the sum of the 20 items comes up with like 200 zeros after it and I can't get them to go away. Can someone please take a look at my attached flash file? The actionscript for the sum of all the columns is at the bottom. Thanks!

emergency_pants
06-29-2004, 09:00 AM
akschinas,

I think the error comes from the way you are adding the ".00" and ".0" strings onto the end of the numbers. It's turning all the numerical variables into strings and when you add them all together, they therefore return a string too! So, "1.00" + "5.00" = "1.005.00".

I've come accross this technote before and I've pasted the function into your movie... it seems to work quite well.

Here's the link:
http://www.macromedia.com/support/flash/ts/documents/decimal_places.htm

I've attached the updated FLA file.

EDIT p.s.
I just noticed an error occurring in the Total cost savings field. it's because the function places a comma in the currency numbers when the amount totals more than 1,000 <--- like that.

To remove the error, change last line of the function to this:

return (dollars.join("")+"."+cents);

akschinas
06-29-2004, 01:41 PM
Thanks Simon. However, it still doesn't seem to work. I get a number like this "165.2" instead of this "165.20". I also put the code you gave me in the last line, but the Total Cost still comes out to NaN. Thanks so much for taking the time to rewrite my code! I didn't expect that at all. You've been a great help.

emergency_pants
06-29-2004, 03:44 PM
yeah... see what you mean. I can see what the problem is... the totals are being concatenated as a string. The only way I can think of to get rid of this is to just multiply them all by 1 when you calculate the totals.

In your original posted version, you can replace totals (IHTC and OSTC)calculations with this:


IHTC = (IHPB*1)+(IHPB2*1)+(IHPC*1)+(IHPC2*1)+(IHPC3*1)+(I HPG*1)+(IHPG2*1)+(IHPG3*1)+(IHHW*1)+(IHHW2*1)+(IHH W3*1)+(IHSG*1)+(IHSG2*1)+(IHSG3*1)+(IHWG*1)+(IHWG2 *1)+(IHWG3*1)+(IHGM*1)+(IHGM2*1)+(IHGM3*1)+(IHFP*1 )+(IHFP2*1)+(IHFP3*1)+(IHPE*1)+(IHPE2*1)+(IHPE3*1) +(IHRC*1)+(IHRC2*1)+(IHRC3*1);
if (Math.floor(IHTC*100)%100 == 0) {
IHTC += ".00";
} else if (Math.floor(IHTC*100)%10 == 0) {
IHTC += "0";
}
// sum of the total numbers in the outsourcing column
OSTC = (OSPB*1)+(OSPB2*1)+(OSPC*1)+(OSPC2*1)+(OSPC3*1)+(O SPG*1)+(OSPG2*1)+(OSPG3*1)+(OSHW*1)+(OSHW2*1)+(OSH W3*1)+(OSSG*1)+(OSSG2*1)+(OSSG3*1)+(OSWG*1)+(OSWG2 *1)+(OSWG3*1)+(OSGM*1)+(OSGM2*1)+(OSGM3*1)+(OSFP*1 )+(OSFP2*1)+(OSFP3*1)+(OSPE*1)+(OSPE2*1)+(OSPE3*1) +(OSRC*1)+(OSRC2*1)+(OSRC3*1);
if (Math.floor(OSTC*100)%100 == 0) {
OSTC += ".00";
} else if (Math.floor(OSTC*100)%10 == 0) {
OSTC += "0";
}

I tested it and it works fine.

If you want the version with the function, as I posted earlier, I've attached an updated version.

akschinas
06-29-2004, 05:46 PM
You're the best! It works beautifully! Thanks for your help.

randocommando
06-30-2004, 07:38 PM
I have a similar situation, but simpler. I am new to flash and am trying to figure out tax, so I am only working with one number. I DL your zip but couldn't get it working. I tried to run trace statements, but nothing was showing. How would I be able to use your example?