Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'd tried this code by these values:
float timeStamp;
a=1338526801
b=113678
timeStamp = a +( b / 1000000);
then I changed the b to 113680 and calculated the timeStamp,
timeStamp = a+ (b / 1000000) ;
in real the timeStamp should change because the b has been changed, but when I print it by console.writeline(), the timeStamp value doesn't change.I think it refers to the precision of the c# values, but I don't know how to resolve it.
You should take a look at Floating-Point Types Table (C# Reference) which gives the following info
> Type Approximate range Precision
> float ±1.5e−45 to ±3.4e38 7 digits
> double ±5.0e−324 to ±1.7e308 15-16 digits
Your combination of 338526801 + 113678/1000000 is about 16 digits and would better fit into a double.
A float which contains 7 digits would get you accuracy to 338526800.000000 and no more
float f = 338526801 + 113678f/1000000
System.Diagnostics.Debug.Print(f.ToString("F6")); // results in 338526800.000000
however a double gets 15-16 digits can actually store the data to your precision.
double d = 338526801d + 113678d/1000000
System.Diagnostics.Debug.Print(d.ToString("F6")); // results in 338526801.113678
You could also look at Timespan and DateTime which give you accuracy to 100-nanosecond units. Since there are 10 ticks in a micro-second (us), the same TimeSpan would be:
TimeSpan time = new TimeSpan(3385268011136780);
One of the comments suggested you might be trying to convert Unix Time. If so then you can add the Timespan to the proper DateTime representing 1/1/1970.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I've read a few posts and I understand how double arithmetic doesn't always return the results you would expect.
Why is floating point arithmetic in C# imprecise?
My problem is different though because I'm checking to see if two numbers are the same and I'm getting false when I would expect true.
I've also read this What Every Computer Scientist Should Know About Floating-Point Arithmetic, but am still having trouble understanding why two seemingly equal double variables are showing unequal.
UPDATE: They answer below helped me understand that the value displayed by the debugger wasn't the 'whole story'. That is why the two floats seemed equal. The debugger was showing equal values when hovering the variables.
Default string format is not precise for float numbers, should use "G17"
double a = 17.125 / 3.0;
double b = 17.12499999999999 / 3.0;
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(a.ToString("G17"));
Console.WriteLine(b.ToString("G17"));
Console.WriteLine(a == b);
Console.WriteLine(Convert.ToString(*(long*)&a, 2));
Console.WriteLine(Convert.ToString(*(*)&b, 2));
Result:
5.70833333333333
5.70833333333333
5.708333333333333
5.7083333333333295
False
100000000010110110101010101010101010101010101010101010101010101
100000000010110110101010101010101010101010101010101010101010001
Usually to compare 2 float numbers, you can use a small error number
Console.WriteLine(Math.Abs(a - b) < 0.0000001);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am attempting to write out to a fixed format text file and am having some trouble formatting the numbers properly. It needs to be padded with leading zeros and an implied decimal.
Example:
43.80
The output would need to be:
0000004380
So far, I have attempted to convert the double to a string, replace the decimal, and then pad with "0".
((amount.ToString()).Replace(".","")).PadLeft(10, '0')
The problem with this is when the number ends with zeros. The above example comes out as:
0000000438
Any suggestions?
decimal value = 34.80M;
int intVal = (int)(value * 100);
return intVal.ToString("0000000000");
You could try:
((string.Format("{0:0.00}", amount)).Replace(".","")).PadLeft(10, '0')
That way you don't lose the 2nd decimal place when the value is 0.
You can pass a .ToString() format string in as described in this article:
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
Does the incoming value always have two decimal places? If so, you could multiply by 100 to get rid of the decimal, and then to .ToString("D10")
(Int)(43.80 * 100).ToString("D10")
This should do the job : $"{(int)(amount * 100):D010}"
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm completely unsure on how to do it. I've searched but can't find a simple answer.
I've done the multiplication and I know its similar to it. Need some help. I want to know how to do division for two fractions.
My Multiply module:
{
answerDenominator = num1Denominator * num2Denominator; //Multiply both denominators
answerNumerator = ((num1Whole * num1Denominator) + num1Numerator) * //multiply the whole number by the denominator and add the numerator to it
((num1Whole * num2Denominator) + num2Numerator); //multiply the whole number by the second denominator, then add the second numerator, multiply these two answers together
answerWhole = answerNumerator / answerDenominator;
answerNumerator = answerNumerator % answerDenominator;
}
Let that we have to make the following division:
(a/b):(c/d)
This is equal to
(a/b)*(d/c)
That being said the division can simply be done like below:
static double CalculateDivisionResult(double a, double b, double c, double d)
{
return (a/b)*(d/c);
}
In the above:
a is the num1Numerator.
b is the num1Denominator.
c is the num2Numerator.
d is the num2Denominator.
The most important thing that you should pay attention on the above is the fact that we use double. Why we do so?
Let that a=3, b=7, c=4 and d=5:
Then
(a/b)*(d/c) = 15/28
If you had chosen to represent your number as integers, int a=3, then the above would be obvious 0. Representing them as doubles we can overcome this.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to show a double value to label (C#) with 2 decimal places no matters that value like 13 or 13.5 or 13.505
Its always show 13.00
You can pass the format in to the to string method
eg:
ToString("0.00"); //2dp Number
ToString("n2"); // 2dp Number
ToString("c2"); // 2dp currency
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
To change, for example, 13.505 to 13.00 you'd also want to run it through Math.Floor or use one of the other suggested methods for rounding down.
Math.Floor(value)
http://msdn.microsoft.com/en-us/library/e0b5f0xb.aspx
If, on the other hand, you want to change 13.505 to 13.50 you'd want to run it through Math.Truncate.
Math.Truncate(Value)
http://msdn.microsoft.com/en-us/library/7d101hyf(v=vs.110).aspx
So to tie that together:
Double testValue = 13.505;
Double testValueTruncated = Math.Truncate(100 * testValue) / 100;
string withDecimalPlaces = testValueTruncated.ToString("0.00");
withDecimalPlaces will now have the value "13.50"
try this method
double i=12.22222; //first variable
double j=1.2545; //second variable
double h=i*j; // multiple first and second
string s=h.ToString("0.00"); // convert to string and proper format
this methed return
s="15.33"
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have this following code to calculate the percentace in perst, but when i run this code I always get o as value in perst for all values of qxount and acount.
int perst;
int qcount;
int acount;
perst = (acount / qcount) * 100;
When dividing integers the result will be an integer. This means that you are expecting a value such as 0.75 (which you appear to be thinking you'll multiply by 100 to get a percentage) then the integer value returned will be only the 0 which is the leading integer. The remainder would be available with the % modulus operator.
However, get a percentage like you seem to want, you'll need to divide using doubles or float values.
double perst;
double qcount;
double acount;
perst = (acount / qcount) * 100;
The MSDN article on the division operator - Good idea to read.
I think you have some integer rounding issues. Try something like this.
int perst;
int qcount = 100;
int acount = 5;
perst = Convert.ToInt32(((double)acount / (double)qcount) * 100);
While rounding is almost certainly the mistake (after assigning values to the variables) the following works just fine:
int a = 62;
int b = 235;
int percentage = 100*a/b;
Console.WriteLine(percentage);
You don't need to use doubles. This rounds the percentage towards zero. If you need more precise results, go with double or single.