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);
Related
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);
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
Decimal Value = " 19500.98"
I need to display this value to TextBox with rounded off like "19501"
If decimal value = " 19500.43"
then
Value = "19501"
If decimal value comes +1 is add.
Just try below code:
decimal convertDecimal = decimal.Parse("19500.43", CultureInfo.InvariantCulture);
decimal ceiling = Math.Ceiling(convertDecimal );
For more information:
DotnetPerls on Math.Ceiling
MSDN
try this on:
decimal value = 1500.98M;
decimal newvalue = Math.Ceiling(value);
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));
What is the best way to format a decimal if I only want decimal displayed if it is not an integer.
Eg:
decimal amount = 1000M
decimal vat = 12.50M
When formatted I want:
Amount: 1000 (not 1000.0000)
Vat: 12.5 (not 12.50)
decimal one = 1000M;
decimal two = 12.5M;
Console.WriteLine(one.ToString("0.##"));
Console.WriteLine(two.ToString("0.##"));
Updated following comment by user1676558
Try this:
decimal one = 1000M;
decimal two = 12.5M;
decimal three = 12.567M;
Console.WriteLine(one.ToString("G"));
Console.WriteLine(two.ToString("G"));
Console.WriteLine(three.ToString("G"));
For a decimal value, the default precision for the "G" format specifier is 29 digits, and fixed-point notation is always used when the precision is omitted, so this is the same as "0.#############################".
Unlike "0.##" it will display all significant decimal places (a decimal value can not have more than 29 decimal places).
The "G29" format specifier is similar but can use scientific notation if more compact (see Standard numeric format strings).
Thus:
decimal d = 0.0000000000000000000012M;
Console.WriteLine(d.ToString("G")); // Uses fixed-point notation
Console.WriteLine(d.ToString("G29"); // Uses scientific notation