StringFormat Double without rounding - c#

double d = toDateTime.SelectedDateTime.Subtract(
servicefromDateTime.SelectedDateTime).TotalHours;
string s = String.Format("{0:0}",d);
But the String.Format rounds up the value: if d is 22.91 the String.Format gives the rounding result of 23. I don't want to round up. For example, if d is 22.1222222, then I want 22. if d is 22.999999, then I want 22.
How can I achieve this?

You could use Math.Truncate
double d = toDateTime.SelectedDateTime.Subtract(servicefromDateTime.SelectedDateTime).TotalHours;
string s = String.Format("{0:0}", Math.Truncate(d));

If you cast the double to an int/long it will chop off any decimal component, effectively giving you a "floor" or round-down of the double.

Then you need to Math.Floor
double d = toDateTime.SelectedDateTime.Subtract(servicefromDateTime.SelectedDateTime).TotalHours;
string s = String.Format("{0:0}",Math.Floor(d));

Related

C# changing double value 0.0 to 0

The value 0.0 is changing to 0 when I assign it to a double variable. Is there any workaround to preserve the decimal and succeeding 0's if any?
Edit:
double dt = 0.0;
Console.WriteLine(dt);
//Output:0
You can provide the desired format:
double dt = 0.0;
Console.WriteLine($"{dt:f1}");
Here f1 stands for 1 digit after decimal point.
As an alternative (esp. if you are working finance) you can change the type, from double to decimal:
decimal dt = 0.0m;
Console.WriteLine(dt);

Decimal point lost in conversion

I have a string that contains: 2.53 and I'm trying to convert this number into decimal type, so I did:
string value = "2.53";
decimal converted = Convert.ToDecimal(value);
but the final result is: 253
Decimal point is different in every culture. In your culture it might be a comma. You can use InvariantCulture which has dot as decimal separator:
decimal converted = Convert.ToDecimal(value, CultureInfo.InvariantCulture);

How to convert the double value to string without losing the data in c#

I have a double value as below
double propertyValue = 1994.7755474452554;
When i convert this double value to a string
string value = propertyValue.ToString();
it gives the value "1994.77554744526"
It has rounded off the last digits 2554 to 26.
But I do not want to round the value. Share your ideas.
By default the .ToString() method of Double returns 15 digits of precision. If you want the full 17 digits that the double value holds internally, you need to pass the "G17" format specifier to the method.
String s = value.ToString("G17");
This will prevent the rounding off of double value when converted to string.
value = propertyValue.ToString("G17");
You could use decimal type instead.
decimal propertyValue = 1994.7755474452554M;
I can't reproduce the misbehaviour; you've experienced a representation effect: it's ToString() puts double like that.
string source = "1994.7755474452554";
double d = double.Parse(source, CultureInfo.InvariantCulture);
string test = d.ToString("R", CultureInfo.InvariantCulture);
Console.Write(source.Equals(test) ? "OK" : "Rounded !!!");
Outcome is OK. Please, notice "R" format string:
The Round-trip ("R") Format Specifier
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#RFormatString
You can cast double to decimal and use the default ToString() like this:
Double propertyValue = 1994.77554744526;
var str = ((decimal)propertyValue).ToString();
//str will hold 1994.77554744526

Convert from exponential notation to non-exponential notation

I have tried:
MessageBox.Show(System.Numerics.BigInteger.Parse("7.56e+011",
NumberStyles.Float,
CultureInfo.InvariantCulture));
But it continues to show 7.56e+011
You are looking to format the number. You can use String.Format to do so
string.Format("{0:F}",System.Numerics.BigInteger.Parse("7.56e+011",
NumberStyles.Float,
CultureInfo.InvariantCulture))
Running the following code
Gives you the following MessageBox
you can specify no decimal points by changing it to {0:F0} for the format.
Try
BigInteger num = System.Numerics.BigInteger.Parse("7.56e+011",
NumberStyles.Float,
CultureInfo.InvariantCulture);
String text = num.ToString("F5"); // New format string, here with 5 digits.
Your solution does an implicit conversion from BigInteger back to string again, which uses the scientific notation if the exponent is large.
decimal dec = decimal.Parse("7.7583877127496407E-6",
System.Globalization.NumberStyles.Any);
Console.WriteLine(dec);

unable to understand decimal in C#

I try:
decimal dd = 4.12345611111111111;
lblText.Text = string.Format("{0:N6}", dd);
but I got a error use an M suffix
decimal dd = 4.12345611111111111m;
its work perfect.I want to know what is m for?.Thanks for clearing my vision.
note: lblText is id of label control in asp.net.
If you don't specify the suffix the default is double for a number with decimal separator. M specify that the literal is actually a decimal
It is a literal used for decimal type. Read more here
TO display decimal value you have to append m or M.
decimal d = 1.04893m;
The m stands for monetary if I'm not mistaken, the f for float, and the d, surprisingly, for double.

Categories