A Flash Developer Resource Site

# Thread: Rounding Numbers *URGENT Help Needed*

1. ## Rounding Numbers *URGENT Help Needed*

My friends,
I come to you once again because I don't know how to fix a bug in a flash projector file (.EXE) that I'm working on.
My software:
It calculates the transport fee (value) for shipments (documents & products) based on its weight. Example: The user types the weight of the shipment into an input txt box and then I set up actions like:
=========================
if (weight<=0.5) {
value = "41.00";
} else if (weight<=1.0) {
value = "47.00";
} AND SO ON

=========================
Now I'll tell you where the bug is...
The price has non-constant increases every for every 0.5 kilo that you add to the total weight until it reaches 50 kilos.
So, after it reaches 50 kilos the price increases constantly and goes up U\$ 2.60for every every 0.5 kilo added. I set up actions like the ones above (else if) from 0.5 to 50 kilos (lot of work).
For the shipments that are over 50 kilos I set up the action:
else if (weight>50) {
value = (((weight-50)*2)*2.60);
}

Translating: If weight is bigger than 50 kilos the price (value) will be ((weight - 50)*2)*2.60
===> example: weight = 60
60 - 50 = 10 kilos
10 * 2 = 20 (why 20???) The price increases every 0.5 kilo, doesn't it? So the correct price increasement would be 20 * 2.60
WHAT IF THE USER TYPES 50.5 (50 KILOGRAMS AND 500 GRAMS) ?????
Than my function won't work properly!!!!
Why??? Let's see: 50.5 - 50 = 0.05
I need to find a way to round the number up like this:
IF weight = 50.0000001 weight = 50.50
IF weight = anything bigger than 50 but smaller than 50.5 than round weight to 50.5
another example:
IF weight = 55.01 or 55.4 or 55.49 or 55.2 than weight = 55.5
WHY THIS???
Well...if weight gets rounded to 50.5:
50.5 - 50 = 0.5 than 0.5 * 2 = 1 *2.60 = add U\$ 2.60 to the price!!

No matter what the weight is, if it's bigger than 50, it should be rounded to the next 1/2 a kilo (.50) for the function to work:
else if (weight>50) {
value = (((weight-50)*2)*2.60);
}
It starts at 50 (weight bigger than 50 and is supposed to work even if the typed weight is 100, 1000, 10.000 kilos....)

*** I need to round these weights because even if the spment is 50.1 kilos we charge 50.5 kilos. Always rounding up.***

We should only round the number up if it's bigger than 50, not equal to...

Did you understand my problem???? How can I help you to understand the BUG in order to help me????
I don't know what to do...
THANK YOU VERY MUCH!!!

2. Let me see if I can tackle this one. Before I do, What is the exact ammount for 50 kilos?

3. Sleeve, I'm glad to see you here my friend!!!
50 kilos = 416.00
Anything heavier than 50 kilos would have an increasement of 2.60 every 0.50 kilo

4. I'm not sure if I understand you or not, but what about something like this???

Code:
```if (weight > 50)
if (weight - Math.floor(weight) < 0.5)
newweight = Math.floor(weight) + 0.5;
else
newweight = Math.ceil(weight);```

5. Look what I got so far: (someone gave me that at the macromedia web forum)

function priceForWeight(kg) {
if (kg<=50) {
return priceForLessThan50kg(kg); // my existing code!
} else {
var rndUp = (Math.ceil(kg*2)/2); // round up to nearest half kilo.
var over50 = rndUp-50.0;
return priceFor50kg+2.60*over50; // fill in the correct value for 50kg!
}
}

Would it work? I didn't try yet...to be honest with you, I 'm not sure how to use that...
Tks once again

6. Pilot_22, that code will work

Place it on your 'calculate' button where
Code:
`return priceFor50kg+2.60*over50;`
is the same as
Code:
`return 416.00+2.60*over50;`

7. My friend " HumanCompiler" && Flashers,
The following code is what I'm looking for, but I need help to make a change because I found a bug or maybe I didn't tell exactly what I needed.
First of all, take a look at the code:
================================================== ===========
if (weight>50) {
if (weight-Math.floor(weight)<0.5) {
newweight = Math.floor(weight)+0.5;
} else {
newweight = Math.ceil(weight);
}
}

================================================== ===========

This is what I got as I problem:
When I described the action that I needed I said that if weight = 50, it should be 50. And the code above changes it to 50.5.
The action would have to be able to understand that I already have a price for 50 kilos and leave it as 50 kilos.
I have an idea, altough I would need your help to get the action.
What if we break the weight into two parts like:
The first part would be the number BEFORE the decimal (comma).
The second part is the what is AFTER the decimal (comma).
This way I can check if the "SECOND PART" is between 0.1 and 0.5 or 0.6 and 0.0.
Why do I need that??? Well, remember that I said that
If weight = 50.00 do nothing
If weight is between 50.01 and 50.50 round to 50.50
if weight is between 50.60 and 51.00 round to 51.00
AND SO ON...
Then, as result we would have the FIRST PART + SECOND PART without a bug.

JUST TO REMEMBER: If the user types any number bigger than 50 I can't calculate the transport value using a price table as I do for weights between 0.00 and 50.00 Kilos. [B]But I do know that my company charges U\$ 2.60 for each 0.50 kilos when the weight is over 50.00.
The result variable has to show me how many 0.50 we have to calculate, then I can just add this value to my final price variable.
This is my email address: jglaser@uol.com.br
ICQ: 96677334 (in case you need further information)

THANK YOU SOOOOOOOO MUCHHHH!!!!!

8. there is an easier way to do this with just a formula:

((Int(((Number(weight)+.4)*10)/5))*5)/10

this should give you the value you need.

I have attached a little movie that converts the numbers as needed.

9. Let me try that.
Just didn't understand what that .4 is for.
Even if it doesn't do exactly what I need, thanks for helping me
I'll let you know in a few minutes if it worked...

10. Phatius,
I tried your file but it doesn't do what I need.
Let me make my self clear.
If the user types any number that is over than 50 kilos (excluding 50) the asctionscript tells me how many half kilos I got over 50 kilos. Why that? Just because the price increases every half kilo and its increasement is equal to U\$ 2.60 for each 1/2 kilo.
The action needs to understand that anything between xx.01 to xx.50 needs to be rounded to the next .50 kilo. && if the number is between .60 and 00 rounds to the next 0.50 (that would end in .00)
Maybe it would be easier to do using > than and < than, woulnd't it?
But I still need to know what is the part of the typed number that comes before the decimal.
Example: if the user types 75.90 I need to get a result like
76 + 00
Would be very nice to get two result variables, one showing the part of the number that comes before the DECIMAL (period) and another result variable showing the rest of the number, the part that comes after the decimal.
*** I just need this new variables for weight that is over 50 kilos (excludind 50) ***
Let me tell you once again why I need these two result variables.
The transport value for 50 kilos is U\$ 416.00 and the price increases U\$ 2.60 every 1/2 a kilo that's added to the weight.
In the first result variable I know how many kilos I have over 50.
In the second one I know how many half kilos I have over 50.

Based on this information I can add the right amount of money to the final price.
**** Does is make sense ****
I really need to get it done, my job depends on it.
Thanks for helping

11. ## the hard part is done

The addition of .4 will causes it to round up. this value only works with tenths of kilos... If you are working with hundreths as well, then number should be .49, and thousandths would be .499 and so on.

So here is what you need to do to get to the vaule. This may not be the exact code you need, but the concept will help get the value you need.

So if weight is > 50

1. get the number rounded up to the nearest 1/2 kilo.

So if weight = 57.2

((Int(((Number(weight)+.49)*10)/5))*5)/10 = 57.5

2. Then subtract 50 to get the number of kilos over 50 round up to the nearest half kilo.

(((Int(((Number(weight)+.49)*10)/5))*5)/10)-50 = 7.5

3. Then to get the number of half kilos multiply by 2 so:

((((Int(((Number(weight)+.49)*10)/5))*5)/10)-50)*2 = 15

You now have 15 half kilos over 50.

4. Then if you multiple by the cost per half kilo (2.60)

(((((Int(((Number(weight)+.49)*10)/5))*5)/10)-50)*2)*2.60) = 39

So now you have \$39.00 over the cost of 50 kilos.

12. ## here is the FLA for that formula

if you need a total, just add the cost for 50 kilos to the total created by this formula.

13. Great.
I´m going to try your fla file when I get home and will let you know if it worked.
Thank a lot!

14. I´m still at work so I didn´t have the time to check your file and see if it works, but I was wondering what that .49 in your formula is for.
Could you please explain that? This way I´ll understand how you got to that formula and then adjust it to my flash file when I get home.
Thanks again!

15. It is pretty simple, the "Int" function rounds the value to the closest whole number, so 50.01 would become 50. by adding .49 50.01 becomes 50.50. But 50 becomes 50.49 and thus gets rounded back down to 50.

Obviously I am simplifying because there are a number of other functions I am applying to the number to get the result. Basically it is the what make the number round up instead of round to the nearest whole number.

16. Dude, dude!!!
It does exactly what I need!!! I'm still testing, trying all the numbers and so far I got NO BUGS at all!!!
Tks, tks, tks, tks, tks!!!!

17. ## Same problem

Phatius or anybody else, pleeeease
I ran into the same problem again and I wish you could just take a look at the problem...
You came up with a formula to solve my problem that was pretty good for 0.50 kilos over 50, do you
remember??
1. get the number rounded up to the nearest 1/2 kilo.

So if weight = 57.2

((Int(((Number(weight)+.49)*10)/5))*5)/10 = 57.5

2. Then subtract 50 to get the number of kilos over 50 round up to the nearest half kilo.

(((Int(((Number(weight)+.49)*10)/5))*5)/10)-50 = 7.5

3. Then to get the number of half kilos multiply by 2 so:

((((Int(((Number(weight)+.49)*10)/5))*5)/10)-50)*2 = 15

You now have 15 half kilos over 50.

4. Then if you multiple by the cost per half kilo (2.60)

(((((Int(((Number(weight)+.49)*10)/5))*5)/10)-50)*2)*2.60) = 39

So now you have \$39.00 over the cost of 50 kilos.
The thing now is that I need the same formula for 1 kilo intead of 0.50 kilo over 50 kilos
I'm using your formula once again, but I couldn't figure what number to use in place of those .49 that
you came up with.
I need the same result, but now we are talking about full kilos, and not half ones.
So, we take that *2 away, right?
If the user types 52 I need the answer to be 2. Pretty easy.
But if the user types any number higher than 52, it must be rounded to the next number, like 53.
Example: 52 = 2 kilos over... 52.3 = 53 --> 3 kilos over
Did you understand my question?????
Tks a lot

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap