69

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

Please help me

3
  • A double rounded will still give you the .00. Instead if you want just 12, you should try casting it to an int, to shed off the decimal part! Look @ Habib's solution for explicit cast!
    – Numan
    Commented Oct 25, 2012 at 6:04
  • Where do you want to "make it as 12"? a.ToString()? Commented Oct 25, 2012 at 6:06
  • stackoverflow.com/questions/4525854/remove-trailing-zeros may this will help you.
    – Hardik
    Commented Apr 21, 2017 at 9:53

8 Answers 8

70

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:

7
  • Hi thanks for your suggestion.I have a number which is having value as 12.00.I want to make it as only 12.I want to remove .00.But i m not able to do as I am getting 12.0 with my code re = Convert.ToDouble(txtModifedTotalAmount.Text.Trim(), CultureInfo.InvariantCulture); if (re - Math.Truncate(re) == 0.00) re = Math.Truncate(re);
    – Divya
    Commented Oct 25, 2012 at 6:05
  • @Divya: They're the same value though - 12, 12.0, 12.00, 12.00000 will all have the same representation as double values. If you mean it's getting displayed as "12.0" when you format it as a string, that's a different matter - and simply part of string formatting. Or if you'll always get an integer and you only want to treat it as an integer, just cast to int or long.
    – Jon Skeet
    Commented Oct 25, 2012 at 6:08
  • i have in sql database decimal(18,3) with input values: 1.000, 11.124, 127.414, 1377.147, Expected output: 1, 11 , 127, 1377, you see i have to remove the scale after decimal, i need to be careful so that i can do calculation using int , so should i cast to int or use Math.Round or split it using string ?
    – Shaiju T
    Commented Jan 20, 2016 at 10:10
  • 1
    @stom: What would you want the result to be for 1.9? Or -0.4? Or -0.6? Note that decimal(18,3) can store numbers bigger than int can...
    – Jon Skeet
    Commented Jan 20, 2016 at 10:16
  • 1
    @stom: Okay, so aside from the range issue, just casting to int should be fine.
    – Jon Skeet
    Commented Jan 20, 2016 at 10:50
45

If you just need the integer part of the double then use explicit cast to int.

int number = (int) a;

You may use Convert.ToInt32 Method (Double), but this will round the number to the nearest integer.

value, rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.

7
  • do you attention do Rounding Error?? Commented Aug 29, 2015 at 9:36
  • 1
    umm what "Rounding Error" ? there would be no rounding in (int) a; (first part), the rounding in Convert.ToInt32 is not rounder error, instead it is the default banker's rounding
    – Habib
    Commented Aug 31, 2015 at 13:02
  • float f = 1.4f; int truncated = (int) ((f-(int)f)*100) ; So, What is your expectation of the truncated variable? 40? Nop, it's 39. Because of the Rounding Error Commented Sep 15, 2015 at 17:20
  • @MehdiKhademloo, the rounding error is due to arithmetic, because "1.4"` isn't really 1.4 (since its a float), (to test it change the type to decimal like decimal f = 1.4m;), but, all of this is not even related to the question asked.
    – Habib
    Commented Sep 15, 2015 at 18:09
  • 3
    @MehdiKhademloo, pure (int) a will work, It will be 1 for 1.4f, exactly 1, there will be no precision point as it is an integer. So no rounding error as far as casting to int is concerned.
    – Habib
    Commented Sep 15, 2015 at 19:12
37

Use Decimal.Truncate

It removes the fractional part from the decimal.

int i = (int)Decimal.Truncate(12.66m)
1
  • This answer should be marked as best answer as it's execution safest Commented Apr 12, 2019 at 20:20
28

Use Math.Round

int d = (int) Math.Round(a, 0);
0
27

Reading all the comments by you, I think you are just trying to display it in a certain format rather than changing the value / casting it to int.

I think the easiest way to display 12.00 as "12" would be using string format specifiers.

double val = 12.00;

string displayed_value = val.ToString("N0"); // Output will be "12"

The best part about this solution is, that it will change 1200.00 to "1,200" (add a comma to it) which is very useful to display amount/money/price of something.

More information can be found here: https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx

0
3

here is a trick

a =  double.Parse(a.ToString().Split(',')[0])
1
  • 3
    That is culture variant and will work only with locales that use comma for decimal separator. This approach is fragile and shouldn't be used. Commented Aug 9, 2019 at 8:47
2

Because the numbers after point is only zero, the best solution is to use the Math.Round(MyNumber)

0

//I am doing a basic Calculation here and this works for me. // it takes values from textboxes and performs the following as far as I understand it. as I have only been coding now for a few months.

double VC2 = Convert.ToDouble(txt_VC_M16_Tap.Text); // converts to double.

double total2 = (VC2 * 1000) / (3.14157 * 14.5); // performs the calculation.

total2 = Math.Round(total2); //Round the result to a whole number(integer)

txt_RPM2.Text = Convert.ToString(total2); // converts result to string and puts it in the textbox as required.

// Hope this helps people that are looking for simple answers.

Not the answer you're looking for? Browse other questions tagged or ask your own question.