68

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

Please help me

Divya
  • 979
  • 3
  • 13
  • 18
  • 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 Oct 25 '12 at 06:04
  • Where do you want to "make it as 12"? `a.ToString()`? – Alexei Levenkov Oct 25 '12 at 06:06
  • http://stackoverflow.com/questions/4525854/remove-trailing-zeros may this will help you. – Hardik Apr 21 '17 at 09:53

8 Answers8

68

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:

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 Oct 25 '12 at 06: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 Oct 25 '12 at 06: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 Jan 20 '16 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 Jan 20 '16 at 10:16
  • for 1.9 i just need 1 and we dont have minus values as its price – Shaiju T Jan 20 '16 at 10:22
  • 1
    @stom: Okay, so aside from the range issue, just casting to `int` should be fine. – Jon Skeet Jan 20 '16 at 10:50
  • @fdsfdsfdsfds: I'm afraid I don't understand what your comment is intending to convey. – Jon Skeet Nov 07 '19 at 17:17
44

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.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • do you attention do **Rounding Error**?? – Mehdi Khademloo Aug 29 '15 at 09: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](http://stackoverflow.com/questions/311696/why-does-net-use-bankers-rounding-as-default) – Habib Aug 31 '15 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** – Mehdi Khademloo Sep 15 '15 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 Sep 15 '15 at 18:09
  • I said the pure `(int) a` can not work well. Just it – Mehdi Khademloo Sep 15 '15 at 18:49
  • 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 Sep 15 '15 at 19:12
  • @MehdiKhademloo, one more thing that you can check is `float f = 1.4f; var result = f - 1;` here the result would be something like `0.399999976` that is the nature of floating point number, It has nothing to do with casting to `int` using `(int)`. See: [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Habib Sep 15 '15 at 19:18
36

Use Decimal.Truncate

It removes the fractional part from the decimal.

int i = (int)Decimal.Truncate(12.66m)
Pawel Cioch
  • 2,895
  • 1
  • 30
  • 29
Faiyaz
  • 1,391
  • 11
  • 9
28

Use Math.Round

int d = (int) Math.Round(a, 0);
Adil
  • 146,340
  • 25
  • 209
  • 204
25

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

vohrahul
  • 1,123
  • 10
  • 17
3

here is a trick

a =  double.Parse(a.ToString().Split(',')[0])
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • 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. – the berserker Aug 09 '19 at 08:47
2

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

RainyTears
  • 171
  • 4
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.