I put an "if" function to give me no decimals if result is >200, if not to give me 1 decimal. It works great, unless the result's decimal is .0, then it doesn't show me my decimal. How can I force the decimal to show?
Here is my function: =IF($E$13>200,ROUND($E$13,0),ROUND($E$13,1))
If representing the number as text is acceptable, the following will solve your problem:
=IF($E$13>200,ROUND($E$13,0),IF(ROUND($E$13,0)=ROUND($E$13,1),CONCATENATE(ROUND($E$13,0),".0"),ROUND($E$13,1)))
If you need to use your number in future calculations, be sure to use the original number, which can be obtained via ROUND($E$13,1).
Related
The value is the result of dividing the sum of 2.01 and 2.52 by 2 (2.01 + 2.52) / 2. Excel displays this value as 2.265 and when formatted to 2 digits, it's 2.27. However, the value stored in the file is 2.2649999999999997. When I recreate this is C#, I also get that value in my variable, not 2.265. I understand this is due to floating point precision issues with the division of 4.53 by 2.
double result = (2.01 + 2.52) / 2;
Console.WriteLine(result);
The Console displays 2.265 but the value shown in the QuickWatch debugger shows 2.264999999999997. I suspect the conversion of the value to a string on the in the WriteLine method is correcting for the floating point precision error.
When I apply Math.Round(result, 2, MidpointRounding.AwayFromZero), the result comes back as 2.26 not 2.27 as I expected. It seems like it looks at the first number to the right of the digit I want rounded, sees it's a 4 and ignores everything else to the right of it. The problem is that those 9's are only there because of the precision problem and need to be included, or better yet, the value should be 2.265.
What I have done in my code, is to read the text value from the Excel spreadsheet "2.2649999999999997", convert that to a double 2.264999999999999 and then to a string, which gives me "2.265". I then convert that back to a double 2.265 so that I can apply the Math.Round to it and get the expected result of 2.27. Here's the full code:
double result = Convert.ToDouble(((2.01 + 2.52) / 2).ToString());
Console.WriteLine(Math.Round(result, 2, MidpointRounding.AwayFromZero));
Is my approach for floating point precision and rounding issues, relying on ToString to clean it up, the correct one? If not, how should I have done it?
First: The problem is hard. Because 4.53/2 = 2.265. This rounds to 2.27. However the tiniest rounding error in the calculation resulting in a smaller result (2.264999999....) will lead to a rounding to 2.26. This is what is happening here.
To solve this problem you need to have a floating point arithmetic which has the same internal rounding errors as Excel does.
From this document https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel it appears as if Excel uses a modified version of IEEE 754, while C# uses IEEE 754. I do not know where the differences are, but it appears as if internally Excel generates different rounding errors.
This document describes the differences: https://support.microsoft.com/en-us/kb/78113/en-us
(For example, Excel does not use denormalized numbers. This implies a different behavior for rounding errors for numbers < 2).
So I assume you cannot solve this using "double"
Update
However, now that I understand that the issue is not the arithmetic, but the way Excel displays the number, maybe this is a solution
Math.Round(Math.Round(result, 3, MidpointRounding.AwayFromZero), 2, MidpointRounding.AwayFromZero)
First round to 3, then to 2 digits. It appears to me that Excel is doing this.
I am very new to Excel and I have a problem with a simple multiplication (I know, it is depressing but I'm stuck).
I have to multiply the numeric content of 2 cells (these value are calculated using 2 different formulas).
The problem is that it seems that these 2 cells contain numeric values having different format and I obtain a strange result.
Infact I have:
1) The K3 cell containing this value: 0,0783272400
2) The K6 cell containing this value: 728.454911165
In another cell I simply do:
=K3*K6
but now I am obtaining this nonsense value: 57.057.862.655,9996000000
I think that the problem could be related to the fact that the first one use the , do divide integer section and decimal section, and in the other one I am using . to divider the integer section and decimal section.
How can I correctly handle this situation?
Format both values as Currency in Excel and forget about the issue.
You are getting it, because the floating point values are not represented differently in many programming languages. In Excel probably the best way to make sure you do not give strange values is to format as Currency.
Or in VBA to use the CDec and to convert to decimal.
Is floating point math broken?
Excel is treating 0,0783272400 as something less than one tenth and 728.454911165 as getting on for one thousand billion. The result is formatted with . for thousands separator and , for decimal separator - and is not nonsensical (though the choice of formatting is).
Wondering if anyone knows a function that would return the number of significant figures after a decimal point? And even further how to put that number in a specific formatting?
For example if the number was 27.9834 it would return 0.0001. Or if it was 2.1 it would return 0.1.
You should be able to do this using a LEN and MATCH to get the number of decimal points, then its a simple "^" function to get the decimal place.
Assuming your number is in Cell A1:
=10^-(LEN(A1) - FIND(".",A1))
Just make sure you are showing the right number of significant digits in the result cell or it will just look like zero.
The LEN() counts the number of characters and then you subtract the number of characters from the left to where the decimal is. I think there is an upper limit on the number of decimals that excel can handle, but i don't recall what it is.
Another method might be,
=AGGREGATE(14, 6, POWER(10, -(ROW($1:$16)-1))/(TRUNC(A2, ROW($1:$16)-1)=A2), 1)
I want to round off the value in Excel when the value is greater than 5 after decimal.
For example:
if num= 9.15, result= 9.1
if num= 9.16, result = 9.2
Although your need contradicts the currently valid rounding rules it could be achieved with the following formula:
=TRUNC($A1*10^1+0.4*SIGN($A1))/10^1
The value in A1 can be any decimal value in any length either positive or negative. It will be "rounded" to 1 decimal place. The 10^1 part in the formula leads to rounding to 1 decimal place. Use 10^2 to round to 2 decimal places and so on.
For the second decimal place, I was going to post
=IF(AND(FIND(".",A2&".")=(LEN(A2)-2),RIGHT(A2)="5"),--LEFT(A2,LEN(A2)-1),ROUND(A2,1))
(modified according to #Jasen's comment)
A very simple approach is
=ROUND(A4-10^-10*SIGN(A4),1)
which should be fine up to several places of decimals if you change the number of decimals to round (but will fail because of rounding errors if the numbers are too large).
This also gives good results over a wide range of numbers:-
=ROUND(A2-A2/10^12,1)
To generalise the first one a bit more you could try
=IF(ISNUMBER(FIND(".",A2)),IF(RIGHT(A2)="5",--LEFT(A2,LEN(A2)-1),ROUND(A2,LEN(A2)-FIND(".",A2)-1)),A2)
to round the last decimal place down if it's a 5.
this should do it
=ceil(A1*10-0.5)/10
I know how to isolate the decimal using the TRUNC() function, as well as taking the original value and subtracting this truncated part.
And I can then multiply to get whole number.
But that only works if all my decimals are the same place. I want something that will get me the amount after the decimal point as a whole number, regardless of how many places.
eg: 12.2 would return 2
12.21 would return 21
With data in A1, consider:
=IF(ISERROR(FIND(".",A1)),A1,--MID(A1,(FIND(".",A1)+1),9999))
Naturally leading zeros in the output are dropped: