This question already has answers here:
Double to string conversion without scientific notation
(18 answers)
Closed 8 years ago.
For example, I have a number 0,000000005 and it's displayed in ListBox like 5E-09. So, I would like it to be displayed exactly 0,000000005. Is there any way to do that? Thanx a lot.
listView1.Items[i].SubItems.Add(Convert.ToString(0.000000005));
Use a format specifier, like in your case:
listView1.Items[i].SubItems.Add(String.Format("{0:F9}", 0.0000005));
Generic examples:
double v = 17688.65849;
double v2 = 0.15;
int x = 21;
Console.WriteLine("{0:F2}", v); // 17688.66
Console.WriteLine("{0:N5}", v); // 17, 688.65849
Console.WriteLine("{0:e}", v); // 1.768866e+004
Console.WriteLine("{0:r}", v); // 17688.65849
Console.WriteLine("{0:p}", v2); // 15.00 %
Console.WriteLine("{0:X}", x); // 15
Console.WriteLine("{0:D12}", x); // 000000000021
Console.WriteLine("{0:C}", 189.99); // $189.99
Related
This question already has answers here:
How to ignore value after first decimal point first value if it is 0 else take it if greater than 0?
(2 answers)
Remove trailing zeros
(23 answers)
Closed 5 years ago.
I am trying to remove last zero of a decimal value search a lot for this but all answers in web and stack overflow removing all zeros. My aim to display some zeros in last.
Sample
50.00000000
50.01000000
50.01010000
50.01011000
50.01010100
expected
50.00
50.01
50.0101
50.01011
50.010101
I dont lnow how to do it in c#.
I was tried many things like .ToString("G29"); and other answers available but its all giving me like
50.00
50.01
50.01
50.01
50.01
Please Help..
Try this:
decimal num = 50.00m;
decimal num2 = 50.01010100m;
Console.WriteLine(num.ToString("0.00#########"));
Console.WriteLine(num2.ToString("0.00#########"));
Output:
50.00
50.010101
(Like suggested in comments by "Alex" and "Erno de Weerd")
You could use TrimEnd to remove the last zeros like this:
decimal num = 50.01011000m;
if (num % 1 == 0)
{
return num.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
}
string str = num.ToString().TrimEnd('0');
Console.WriteLine(str);
output:
50,01011
You could also pack it into an extension method:
public static class Extensions
{
public static string ToTrimmendString(this decimal num)
{
if (num % 1 == 0)
{
return num.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
}
return num.ToString().TrimEnd('0');
}
}
and call it like this:
Console.WriteLine(num.ToTrimmendString());
You can try
double num = 50.010110000;
Console.WriteLine(num.ToString("0.0000", CultureInfo.InvariantCulture) );
Please see this for more info https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#SpecifierPt`
you can try something like this
public decimal removeTrailingZeros(decimal val)
{
return val % 1 == 0 ? Math.Round(val, 2) : val / 1.0000000000000000000000000000000000m;
}
test input
50.00000000m,
50.01000000m,
50.01010000m,
50.01011000m,
50.01010100m,
output
50.00
50.01
50.0101
50.01011
50.010101
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 5 years ago.
Look at this situation:
var number1 = Math.Floor(1.9999999999999998d); // the result is 1
var number2 = Math.Floor(1.9999999999999999d); // the result is 2
In a both cases, the result should be 1. I know it's a very unlikely scenario, but possible to occur. The same ocurr with Math.Truncate method and (int) cast.
Why does it happen?
There is no exact double representation for a lot of numbers.
The double number the nearest from 1.9999999999999999 is 2, so the compiler rounds it up.
Try to print it before using your Math.Floor function !
However, the nearest from 1.9999999999999998 is still 1.something, so Floor gives out 1 .
Again, it would be enough to print the number before the function Floorto see that they were actually not anymore the one entered in the code.
EDIT : To print out the number with most precision :
double a1 = 1.9999999999999998;
Console.WriteLine(a1.ToString("G17"));
// output : 1.9999999999999998
double a2 = 1.9999999999999999;
Console.WriteLine(a2.ToString("G17"));
// output : 2
Since double precision is not always precise to 17 significative digits (including the first one before the decimal point), default ToString() will round it up to 16 significant digits, thus, in this case, rounding it up to 2 as well, but only at runtime, not at compile time.
If you put literals values into another variables, then you see it why:
var a1 = 1.9999999999999998d; // a1 = 1.9999999999999998d
var number1 = Math.Floor(a1);
Console.WriteLine(number1); // 1
var a2 = 1.9999999999999999d; // a2 = 2
var number2 = Math.Floor(a2);
Console.WriteLine(number2); // 2
As for why - this has to be something to do with precision of double and decision of compiler as to what value to use for a given literal.
This question already has answers here:
C# Getting strange results for a simple math operations
(4 answers)
Is floating point math broken?
(31 answers)
Closed 5 years ago.
Why (0.406 * 10000.0) returns 4060.0000000000005 instead of 4060.0 in C#
I have written a function which checks no. of decimals in a double value and below is the code I am using. The problem described in the above sentence occurs when value of d is 0.406 and values of n is 4 and the function returns true instead of false
I am open to using alternate solution.
public static bool HasMoreThanNDecimals(double d, int n)
{
return !(d * (double)Math.Pow(10, n) % 1 == 0);
}
Just use decimal type instead of double for more precision to get the desired result.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/921a8ffc-9829-4145-bdc9-a96c1ec174a5/decimal-vs-double-difference?forum=csharpgeneral
This question already has answers here:
Always return positive value
(9 answers)
Closed 5 years ago.
I am calculating the difference between two numbers. If the calculation ends up being 5 - 10, it equals to "-5". If this is the case I need results to display/equal to "+5" , with the "+" sign.
I basically need reverse. So same if 10 - 5 quals to "5" I need it to display as "+5"
Code below I am using:
float rowresults = ROW1 - ROW2;
Textbox.text = rowresults.ToString();
Math.Abs is what you are looking for:
float rowresults = Math.Abs(ROW1 - ROW2);
And to add the "+"-sign to the front of the text (without changing your elsewise existing behaviour):
Textbox.text = "+" + rowresults.ToString();
This question already has answers here:
How do you round a number to two decimal places in C#?
(15 answers)
Closed 8 years ago.
I want to get the round off value of a decimal number Suppose I am getting 24.86 than i want to get 25 as final value
Look at Math.Round(decimal) and the overload which accepts a MidpointRounding argument.
Simply
Math.Round(24.86)
This will round you value to 25.
Your own logic will be
decimal d = 1.5m;
decimal r = d - Math.Truncate(d);
if (r > 0)
r = 1 - r;
decimal value = d + r;