Why C# decimal.ToString behaves differently when literal has .0 [duplicate] - c#

This question already has answers here:
Why does a C# System.Decimal remember trailing zeros?
(3 answers)
Closed 2 months ago.
Executed in c# interactive
> 200M.ToString()
"200"
> 200.0M.ToString()
"200.0"
> 200.000000000000M.ToString()
"200.000000000000"
I was mind blown by this. Why this happens?

200 and 200.0 are the same numerically (200m == 200.0m returns true), of course, but "200" and "200.0" are certainly not the same string, and certainly will not pass a string.Equals() check
Decimal class always retains things like precision and scale. Decimal is intended for monetary calculations, so we definitely want it to exhibit this sort of meticulous behavior

Related

C# unexpected result when casting to float [duplicate]

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.

Conversion of milliseconds to seconds is not working in c# [duplicate]

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

How to determine the # of decimals in a double? [duplicate]

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
;

string.format() with at least one decimal place [duplicate]

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

Format number with comma separator for thousands using C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
String.Format an integer to use 1000's separator without decimal places or leading 0 for small integers
The blog post
http://blog.stevex.net/string-formatting-in-csharp/
(in the Custom number formatting section) shows that using the format {0:0,0}, a number like 1500 will be formatted as 1,500 which is good. But I don't understand why 0 is formatted as 00
Do I need to handle the case of 0 separately which doesn't seem to be necessary.
You can use "F0", which is "fixed number with 0 decimal places". It give you a thousands separator when you results go over 1,000.

Categories

Resources