Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I got a task, for example if there is a number 85.88 and the second number is 0.22. How do i subtract 0.22 and add it to 85.88 so that it turns exactly 86 and the second value turns 0.10?
(I need to know how to do it for different values like: 22.15 and 0.99... and so on)

What I have tried:

Everything that was in my mind
Posted
Updated 9-Oct-18 12:06pm
v2
Comments
Patrice T 1-Oct-18 14:06pm    
Try to paste real assignment and code you have done.
then explain your problem.
OriginalGriff 1-Oct-18 14:06pm    
What have you tried?
Where are you stuck?
What help do you need?
Nelek 1-Oct-18 15:45pm    
But you didn't try what is in our mind... that should work fine.

Although problem statement is not clear stated.

I think you want to determine adjustment figure to make fraction full.

1. First use math.round() to get to get desired rounded figure (86).
2. then Subtract rounded figure(86) and actual figure(85.88) to get second figure(.22)

with above steps you can find out second figure of any fraction to achieve the rounded figure ( generally put in bills before final total to show adjusted amount )
 
Share this answer
 
Comments
Richard Deeming 3-Oct-18 12:54pm    
Math.Round can round down (towards zero) as well as up (away from zero).

Perhaps you meant Math.Ceiling?
K-SIS 21-Oct-18 3:58am    
Question is not clearly framed. If you want only next Integer then Math.Ceiling is correct as you say. Generally in Billing in our country rounding is more adopted so I used Round function.
There is not a solution for the general problem.
In the first case (85.88 and 0.22) you are lucky: 0.12 does the trick.
On the other hand, I cannot see a solution for secondo case (22.15 and 0.99).
 
Share this answer
 
v2
It sounds like you're trying to take the higher number and increase it upward to the nearest integer and then subtract that same amount from the smaller number.

Example:
84.75 and 0.32 would give you 84.75 + .25 and 0.32 - 0.25, which in turn gets you 85 and 0.07.

If that's the case, you could do the following:

double highNum = 84.75; // Or any other first number
double lowNum = 0.32; // or any other second number
double newHighNum = Math.Ceiling(highNum);
double newLowNum = lowNum - (newHighNum - highNum);

Console.WriteLine($"Started with: {highNum} and {lowNum}" );
Console.WriteLine($"Ended with: {newHighNum} and {newLowNum}");
 
Share this answer
 
Quote:
I got a task, for example if there is a number 85.88 and the second number is 0.22. How do i subtract 0.22 and add it to 85.88 so that it turns exactly 86 and the second value turns 0.10?
(I need to know how to do it for different values like: 22.15 and 0.99... and so on)

This is not a requirement, you show a sample input and output, but you forgot to tell the goal and rules.
With what you told us, answer to second example 22.15 and 0.99 can be:
- 22.27 and 0.87
- 23.04 and 0.10
- 23.00 and 0.14
and anything in between.

We just can't guess what you didn't told us.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900