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
Related
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 6 years ago.
I am measuring serverPerformance.
Right now I get this in milliseconds but I want to convert this into seconds when it is more or equal to 500 milliseconds.
I Accomplish this like this
public static string ConvertMillisecondsToSeconds(long milliseconds)
{
if(milliseconds >= 500)
return Math.Ceiling(TimeSpan.FromMilliseconds(milliseconds).TotalSeconds).ToString() + "s";
return milliseconds.ToString() + "ms";
}
My problem
When I return totalseconds without Math.ceiling I get for example:
0,846 seconds
When I use math.Ceiling method I get 1 second.
Desiered result
0,8 seconds.
basically im searching for a method that will return a decimal value, with 2 decimals.
var milliseconds = 0.846;
milliseconds.ToString("0.00"); // Gives 0.85
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
This question already has answers here:
Identify if a string is a number
(26 answers)
Regex for numbers only
(20 answers)
Closed 8 years ago.
How to check the string is number or not. I am verifying mobile number codes in which it should have 10 digits and only in numerical format.
string str="9848768447"
if(str.Length==10 && Here I need condition to check string is number or not)
{
//Code goes here
}
I am new to programming. please help me
Use int.TryParse:
int i;
if(str.Length==10 && int.TryParse(str, out i))
{
//Code goes here
}
Another way which has issues with unicode digits is using Char.IsDigit:
if(str.Length==10 && str.All(Char.IsDigit))
{
}
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;