Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove decimal part from a number in C#

Tags:

c#

I have number of type double. double a = 12.00 I have to make it as 12 by removing .00

Please help me

like image 524
Divya Avatar asked Oct 25 '12 06:10

Divya


People also ask

How do you remove the decimal part of a number?

Step 1: Write down the decimal divided by 1. Step 2: Multiply both top and bottom by 10 for every number after the decimal point. (For example, if there are two numbers after the decimal point, then use 100, if there are three then use 1000, etc.) Step 3: Simplify (or reduce) the Rational number.

How do you find a fractional part of a number in C?

In the C Programming Language, the modf function splits a floating-point value into an integer and a fractional part. The fraction is returned by the modf function and the integer part is stored in the iptr variable.

How do you cut off decimals in C++?

The trunc() function rounds x towards zero and returns the nearest integral value that is not larger in magnitude than x. Simply, the trunc() function truncate the value after decimal and returns the integer part only.

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.


1 Answers

Well 12 and 12.00 have exactly the same representation as double values. Are you trying to end up with a double or something else? (For example, you could cast to int, if you were convinced the value would be in the right range, and if the truncation effect is what you want.)

You might want to look at these methods too:

  • Math.Floor
  • Math.Ceiling
  • Math.Round (with variations for how to handle midpoints)
  • Math.Truncate
like image 125
Jon Skeet Avatar answered Oct 16 '22 19:10

Jon Skeet