I have number of type double. double a = 12.00 I have to make it as 12 by removing .00
Please help me
I have number of type double. double a = 12.00 I have to make it as 12 by removing .00
Please help me
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
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.
Use Decimal.Truncate
It removes the fractional part from the decimal.
int i = (int)Decimal.Truncate(12.66m)
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
here is a trick
a = double.Parse(a.ToString().Split(',')[0])
Because the numbers after point is only zero, the best solution is to use the Math.Round(MyNumber)
//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.