I want to receive the number after the decimal dot in the form of an integer. For example, only 05 from 1.05 or from 2.50 only 50 not 0.50
Solution without rounding problem:
double number = 10.20;
var first2DecimalPlaces = (int)(((decimal)number % 1) * 100);
Console.Write("{0:00}", first2DecimalPlaces);
Outputs: 20
Note if we did not cast to decimal, it would output
19
.
Also:
318.40
outputs: 40
(instead of 39
)47.612345
outputs: 61
(instead of 612345
)3.01
outputs: 01
(instead of 1
)If you are working with financial numbers, for example if in this case you are trying to get the cents part of a transaction amount, always use the
decimal
data type.
Update:
The following will also work if processing it as a string (building on @SearchForKnowledge's answer).
10.2d.ToString("0.00", CultureInfo.InvariantCulture).Split('.')[1]
You can then use Int32.Parse
to convert it to int.