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
var decPlaces = (int)(((decimal)number % 1) * 100);
C++ modf() The modf() function in C++ breaks a number into integral and fractional part. As mentioned, modf() breaks a number to integral and fractional part. The fractional part is returned by the function and the integer part is stored in the address pointed by pointer passed to modf() as argument.
The first digit after the decimal represents the tenths place. The next digit after the decimal represents the hundredths place. The remaining digits continue to fill in the place values until there are no digits left.
Using the modulo ( % ) operator The % operator is an arithmetic operator that calculates and returns the remainder after the division of two numbers. If a number is divided by 1, the remainder will be the fractional part. So, using the modulo operator will give the fractional part of a float.
the best of the best way is:
var floatNumber = 12.5523; var x = floatNumber - Math.Truncate(floatNumber);
result you can convert however you like
var decPlaces = (int)(((decimal)number % 1) * 100);
This presumes your number only has two decimal places.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With