For example if I divide 1050 / 256 I get 4.1015625. I need the value of first digit after decimal point (1 in this case). I don't want to involve ToString() conversions and then parsing it into digit again.
Asked
Active
Viewed 5,087 times
-2
-
1`(int)(4.1015625 * 10) % 10`? – itsme86 Jun 29 '18 at 15:23
-
@itsme86 - `1050 / 250 = 4.2` Then `(int)(4.2 * 10) % 10 = 1`. – Kosmo零 Jun 29 '18 at 15:27
-
Do you want to round or truncate? should `4.16` by `4.1` or `4.2`? – Liam Jun 29 '18 at 15:27
-
@Kosmos How do you get 1? 4.2 times 10 is 42 modulo 10 is 2. – itsme86 Jun 29 '18 at 15:29
-
@Liam - I want value of first digit after decimal point. 1050 / 250 is no alternative equal to 4.2. I want that 2 in that case. – Kosmo零 Jun 29 '18 at 15:30
-
@itsme86 - I uploaded picture how I get 1. – Kosmo零 Jun 29 '18 at 15:33
-
You've got your parentheses wrong. `(int) (test * 10 % 10)`. – Jeroen Mostert Jun 29 '18 at 15:35
-
@Jeroen Mostert - I uploaded picture for you in main question. – Kosmo零 Jun 29 '18 at 15:38
-
Not to pick at nits but in the text of your post you divide by 25**6** while in your pictures for @itsme86 and Jeroen you divide by 25**0**. It doesn't really affect anything but it sure is distracting. – Frank Boyne Jun 29 '18 at 15:41
-
@FrankBoyne - I looking for general solution. Not the one that works just for 1050 / 256. That's why I test different values and show that results are incorrect. – Kosmo零 Jun 29 '18 at 15:43
-
Specifically [this answer](https://stackoverflow.com/a/10079632/542251) – Liam Jun 29 '18 at 15:44
-
Yes, rounding is fun, isn't it? It's not clear why you're opposed to `.ToString()`, as it actually performs the rounding you seem to need. Otherwise, switch to a type like `decimal`, which supports decimal floating-point and can represent this calculation exactly. Or manipulate things to reduce the possibility for rounding to muck things up, like moving up multiplications (`10500f / 250f`). – Jeroen Mostert Jun 29 '18 at 15:47
-
@Liam - answers there says how to truncate two decimal places without rounding. How it can help me if I get 1.99999809? Ok, I truncate something and get 1.99 that still 1 in the end. – Kosmo零 Jun 29 '18 at 15:53
-
Did you try [this answer like I suggested](https://stackoverflow.com/a/10079632/542251)? You can specify the number of decimal places so you just need to specify `1` – Liam Jun 29 '18 at 16:02
-
@Kosmos Your unexpected insistence on using float instead of double isn't helping your rounding issues. If you used double as you suggested you were in your original post instead of float your contrived example would have given you 2 as expected instead of 1. Using decimal would make it even more reliable. – itsme86 Jun 29 '18 at 16:04
-
Thanks everyone who commented. I found a way to get 4.2 after division in Liam's link. – Kosmo零 Jun 29 '18 at 16:11
3 Answers
1
What about:
decimal result = 4.1015625m;
result = result - (int)result;
result = Decimal.Round(result, 1, MidpointRounding.AwayFromZero);
This can be combined into one line if needed but is more readable this way.

ivcubr
- 1,988
- 9
- 20
- 28
-
-
This does not work on a result of division 1050 / 250 and still gives 1, while I expect 2. P.S. -1 is not from me. I glad you trying to help – Kosmo零 Jun 29 '18 at 15:46
-
@Liam yes it produces 0.1 I updated it to make it look a bit better by casting in the second line rather than a `Decimal.Round()` – ivcubr Jun 29 '18 at 16:39
-
@Kosmos Make sure you are getting 4.2 when dividing 1050 / 250. I ran it again with the first line being `decimal result = (decimal)1050 / 250` and got the expected 0.2 – ivcubr Jun 29 '18 at 16:40
-
@Kosmos: Remember, division involving integers always rounds to an integer. Use `1050m / 250m` if you want the division to be in decimals, `f` for float, `d` for double. ("M for money" is the way to remember it.) – Eric Lippert Jun 29 '18 at 19:22
0
Math.Floor((4.1015625 - Math.Floor(4.1015625)) * 10)

Liam
- 27,717
- 28
- 128
- 190

TheBigJobbie
- 34
- 2
-
1This does not work on a result of division 1050 / 250 and still gives 1, while I expect 2. P.S. -1 is not from me. I glad you trying to help. – Kosmo零 Jun 29 '18 at 15:44
0
I would do it this way:
First i would subtract the the number before the decimal seperator of the given number. Then I would multiply it by 10. Example: 4.2 - 4 = .2 * 10 = 2
NOTE: You cannot use floating point numbers for accurate mathematical operations, as some values cannot be represented properly. So always cast to/use decimal and not float/double if you need exact values like this.

Tim
- 2,878
- 1
- 14
- 19
-
This is good in theory, but in code it does not work, because 4.2 - 4 is 1.999999809 O_o Then I multiply it by 10 and get 1; P.S. -1 is not from me. – Kosmo零 Jun 29 '18 at 16:03