[Webinar] Streamline your web hosting managementRegister Today

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 396

round it to .99

Hello,

I am doing some calculation to find the produt price, after doing the calculation I would want the total cost to end as .99

for ex: Return (valel / 1.9) +  ((13.95 / 1.9) + (56 + 1.9))  X 9

if the result for the above come as 54.30 then I would want it to come as 54.99

or 54.01 to 54.99
or 54.98 to 54.99

Thanks
C
0
charlie324
• 5
• 3
• 2
• +3
4 Solutions

Commented:
Hi,

You can try the following:

Return Math.Ceiling((valel / 1.9) +  ((13.95 / 1.9) + (56 + 1.9))  X 9) - 0.01
0

Commented:
Alfred1's solution should work.  The way I would probably do it is:

Return CInt((valel / 1.9) +  ((13.95 / 1.9) + (56 + 1.9))  X 9) + 0.99
0

Author Commented:
Thanks folks, the curect statment is this, please can you advise if i can still round it as desired..

Return Math.Round(((actual / 1.4) + 2 + ((14.95 / 1.4) / moq) + (7.5 / 1.4)) * 2, 2)
0

Commented:
Yes.  @quizwedge's solution should work as well. :-)

The approach is just different.
0

Author Commented:
If I use this

Return CInt(((actual / 1.4) + 2 + ((14.95 / 1.4) / moq) + (7.5 / 1.4)) * 2) + 0.99

I am getting 86.99 instead of 85.99

and when I use this

Return Math.Ceiling(((actual / 1.4) + 2 + ((14.95 / 1.4) / moq) + (7.5 / 1.4)) * 2) - 0.01

then its 85.99 as desired...

0

Author Commented:
values are

Return Math.Ceiling(((34.73/ 1.4) + 2 + ((14.95 / 1.4) / 1) + (7.5 / 1.4)) * 2) - 0.01

would this method gives me correct values at all times?

tahnks
0

Commented:
Use  Math.Floor like below

``````double d = 54.01;
d = Math.Floor(d) + 0.99;
``````
0

Commented:
Hi,

Oh yeah.  Didn't notice that one.  :-)
0

Commented:
Hi,

Yes.  That Math.Ceiling should do the trick.
0

Commented:
Hi,

By the way, why do you need to do this?

Return Math.Round(((actual / 1.4) + 2 + ((14.95 / 1.4) / moq) + (7.5 / 1.4)) * 2, 2)

Is there a reason behind the use of the Round since your requirement is to append a .99 cent value for each product price?
0

Commented:
The above solutions will set all values with  .99....
What value do you want if it is whole number i.e. 54.00 ? go to 54.99 ?
Check this if you want 54.00 to remain 54.00

decimal x = 54.01m;
decimal fract = x - Decimal.Truncate(x);
if (fract > 0)
{
x = Decimal.Truncate(x) + 0.99m;
}
0

Commented:
You can also use mod to get the remainder and then add the difference from 0.99 to the original total.

Dim val as Double = 53.30
val += 0.99 - Math.Round(val Mod 1, 2)

However, as guru_sami mentioned this will always round up to 0.99. If a value is 53.00 it will become 53.99. If that is not the expected output then you would need something like this:

Dim val As Double = 53.01
If (val Mod 1) > 0 Then
val += 0.99 - Math.Round(val Mod 1, 2)
End If
0

Commented:
Not sure why mine didn't work, but go ahead and use one of the other ones.  I was just offering another method in case doing it that way made more sense to you and scored me a few points. :)
0

Featured Post

• 5
• 3
• 2
• +3
Tackle projects and never again get stuck behind a technical roadblock.