Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the decimal part from a double

Tags:

c#

.net

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

like image 1000
user1095549 Avatar asked Oct 23 '12 20:10

user1095549


People also ask

How do you find the decimal part of a double?

var decPlaces = (int)(((decimal)number % 1) * 100);

How do I get the decimal part of a double in C++?

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.

How do you find the part of a decimal?

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.

How do you get the decimal part of a float?

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.


2 Answers

the best of the best way is:

var floatNumber = 12.5523;  var x = floatNumber - Math.Truncate(floatNumber); 

result you can convert however you like

like image 82
matterai Avatar answered Sep 28 '22 21:09

matterai


var decPlaces = (int)(((decimal)number % 1) * 100); 

This presumes your number only has two decimal places.

like image 40
tmesser Avatar answered Sep 28 '22 21:09

tmesser