How to remove the last number from an integer?

11

How can I remove the last number from an integer?

Ex:

int value = 123456789;

int newValue = 12345678;

Note that the new value does not have 9.

I know it would be possible to change to a string and use the substring (), but I would like something without having to convert to string and then have to change back to int .

    
asked by anonymous 24.08.2017 / 14:54

1 answer

17

Pure mathematics:

123456789 / 10

Because the value is integer there is an automatic decimal rounding. If it was a type that has decimal places there would have to do a little more, but you can always do it only in mathematics.

See running on .NET Fiddle . And at Coding Ground . Also put it in GitHub for future reference .

    
24.08.2017 / 14:55