Click here to Skip to main content
15,886,019 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want the code to know know how many steps there is to the closest ten. Ex: if I have 12 the answer should be 8 because 12+8 is 20, if I have 48 the answer should be 2 because 2+48 is 50. How do I do that in code. (C#)


I should add: I have the number 89478598322, then I want the other number you already know (12) and the amount up to the closest 10 should be the same amount as from the last digit in the number"89478598322" (2).

What I have tried:

I have tried using Substring and if functions, but I don't really know how I should do this.
Posted
Updated 21-Sep-19 0:23am
v2
Comments
Patrice T 21-Sep-19 0:28am    
Show your code.
closest ten for 12 is 10, so 2 step backward.
[no name] 21-Sep-19 1:07am    
I know but it has to go up
MadMyche 21-Sep-19 1:33am    
Then you are looking for the next multiple of ten; and not the closest
Richard MacCutchan 22-Sep-19 3:55am    
Replying “will not work” to every suggestion will not help you. Explain why it will not work; which is likely impossible since every solution will work.

Start by converting the strings to numbers:

C#
string input = "38";
int value;
if (!int.TryParse(input, out value))
   {
   ... bad value: tell the user it's not a number! ...
   return;
   }
// From here, you can use value as a number.

Now you have it as a number, you can decide how many to the nearest 10. That's pretty easy: if you use the modulus operator "%" it will return the remainder of a division. So 38 modulus 10 will return just the "lowest digit": 8. You can then use if to check if you want to go up to 40 and return 10 - n or down to 30 and return n
 
Share this answer
 
Comments
[no name] 21-Sep-19 23:27pm    
Will not work!
[no name] 23-Sep-19 17:13pm    
Of course it won't. This will only parse the input if its numerical, and pass it to the value variable. From here you are expected to add the logic to check which values are closest to 12 inside the if condition. Note if its not numerical, the bool will fail silently. Ditch the ! point
The key for you is to know your basic mathematic operations; and write it out.
Then do it in code; using the terms from the above equation.

Less than 5 lines of code to solve as you described or Patrice commented; with "special" exceptions taken care of
Arithmetic operators - C# reference | Microsoft Docs[^]
 
Share this answer
 
Comments
[no name] 21-Sep-19 23:27pm    
Will not work!
MadMyche 22-Sep-19 8:08am    
Give a case in which this does not work- I have it setup on DotNet Fiddle right now
[no name] 23-Sep-19 20:29pm    
I think it would help to study these too : https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators

The fact that the number is to the nearest 10 simplifies things considerably. All you need to consider is the last character in the text expressed as a digit then subtract it from 10 and you're done, assuming you want the number to the next multiple of 10.

 
Share this answer
 
Comments
[no name] 21-Sep-19 23:27pm    
Will not work!
George Swan 23-Sep-19 13:28pm    
Can you plese elucidate? Is 'Will not work' a personal statement?

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