This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 6 years ago.
I am trying to show the time taken by a process in label.
So I implemented watch.
Now let say process took 7 miliseconds and I want to show it in seconds so I wrote 7/1000 which should be 0.007 but its showing 0.
I am showing it into label, so if any conversions of string can show this format please suggest me.
You're not posting any code, but I suppose that you divide two integer values. Integer division always results in an integer as well.
If you divide 7/1000.0 instead (and/or cast at least one operand to a floating-point number, e.g. double) the division will give you the expected result.
You are probably using an int which will not have decimal points. try and change it to a double.
The simplest fix here would be to change your calculation to
seconds/1000.0
Related
This question already has answers here:
Why is floating point arithmetic in C# imprecise?
(3 answers)
Closed 1 year ago.
Try this:
(float)100008009
And you will probably get
100008008
The issue is that we get no warning. And this can't be overflow since floats can take higher values. So I can't explain this result.
What is the Max value for 'float'?
The issue is that we get no warning.
Floating-point is intended to approximate real-number arithmetic. So rounding during conversion is part of the design, meaning it is normal, so it does not get a warning. The closest value to 100008009 representable in float is 100008008, so that is the result.
This question already has answers here:
How to remove decimal part from a number in C#
(8 answers)
Closed 2 years ago.
I have some variable length decimal numbers for ex:
1.123,
1.1234,
12.12345,
I need only the first 4 decimals. I can't use Math.Round() because I do not want rounded numbers, I just want to trim it and keep only first 4, like 1.1234, or 22.1234. Is there a way to accomplish this?
Thank you.
Multiply by 10000, store as integer to get rid off any remaining decimals. Divide by 10000.
var result = number.ToString("n4");
check Standard numeric format strings
This question already has answers here:
Formatting a double to two decimal places
(8 answers)
Closed 6 years ago.
I have a small question regarding Math.Round function.
I need the string "12.123456" to be rounded at 4 decimals. I used:
Math.Round(Convert.ToDouble(pData), 4).ToString()
where pData is defined as string, but the values are decimal with 7 decimals.
My problem is that I expected to get every time the exact 4 decimals, but for some values it gives me only 2 (eg. 12.12 instead of 12.1200).
How can I change in order to always get the needed 4 decimals?
Regards,
You should use format strings instead:
pDate.ToString("0.0000")
or
pDate.ToString("n4")
This question already has answers here:
Find number of decimal places in decimal value regardless of culture
(20 answers)
Closed 8 years ago.
If I have a number, how can I determine the number of decimals?
e.g. for 0.0001 I want to get the result 4
The duplicate suggested above is less suitable than this one because
they are taking about culture-independent code but this question is
just about decimal oriented code (i.e. after the decimal). So no need
to introduce any more overhead:
Finding the number of places after the decimal point of a Double
but they both are good threads.
You can't really. A double is a floating point precision data type, so it's never precise.
You could hack something around, using ToString:
double d = 0.994562d;
int numberOfDecimals = d.ToString(CultureInfo.InvariantCulture).Length
- d.ToString(CultureInfo.InvariantCulture).IndexOf('.')
- 1
;
This question already has answers here:
Display Float as String with at Least 1 Decimal Place
(4 answers)
Closed 7 years ago.
From the following two - double precision - numbers:
123456
0.0003232
I need to get (at least one decimal place) ...
123456.0 (one decimal place added)
0.0003232 (same as above)
... and never scientific notation, like E+000. The more close result from standard string.Format() is string.Format("{0:F1}", myDoubleVal) but in the second case the decimals are lost.
What else can I try?
Thanks.
Try the below
string.Format("{0:0.0###########}",myval);
Thanks