I am attempting to do some calculations in Excel on numbers that include long decimals.
However, Excel doesn't seem to let me populate the cells with the numbers I would like to use.
Example:
If I enter 600000.00000000030000000000 into a standard cell on a new
spreadsheet, it gets converted to 600000. This makes sense as the
cell format is set to General.
If I set the cell format to Number and set the decimals
places to 20, I would expect to be able to enter the number properly.
Instead, the 600000.00000000030000000000 gets converted to 600000.00000000000000000000.
Does anyone know why this would happen?
Excel only stores 15 significant figures for numbers. Any figures beyond that automatically get rounded, regardless of number format.
Setting the cell to Text format first will store the number as a string with as many digits as you want, but Excel can't perform any calculations on it.
However, VBA can do calculations with Decimal type variables which can store up to 29 significant figures.
If you first store the values as text in Excel (setting the cell number format to Text before entering the values), you can create a User Defined Function in VBA to read the string values, convert them to Decimal values, perform your calculations and then return a string with the full precision calculated.
For example:
Function PrecisionSum(ra As Range) As String
'Declare variables holding high precision Decimal values as Variants
Dim decSum As Variant
'This loop will sum values from all cells in input range
For Each raCell In ra
'Read values from input cells, converting the strings to Decimals using CDec function
decSum = decSum + CDec(raCell.Value)
Next raCell
'Return calculated result as a String
PrecisionSum = Format(decSum, "0.00000000000000000000")
End Function
You'll need to write functions to do the operations that you desire.
Note that you'll still be limited by the accuracy of any functions you use in VBA. For example, the SQR function to return the square root of a number only returns a number with Double precision regardless of the precision of the input.
This has to do with the number precision Excel can handle. I noticed that I can show the .00000000030000000000 portion just fine if you remove the integer part. Maybe you could have a separate column for your integer, do your calculations on the decimal part and afterwards add the integer part again.
You can check the wiki regarding this issue - by adding the 600,000, you are essentially adding 6e5+3e-8, which Excel has to work with cummulative bit depth.
Numeric precision in Microsoft Excel
You could try to use exponential (Scientific) notation?
Related
Based on this comment fom SoftTimur I did some testing on formatting and came across the following problem:
If I put =SEQUENCE(4,,-2) in A1 and custom format the range with 0;-0; it'll show the values as I intended.
If I sum the spill range in A6 using =SUM(A1#) it shows the correct value (-2).
If I put the following in B1: =TEXT(SEQUENCE(4,,-2),"0;-0;") I expected the same result as above. However Excel sees it as text (by default aligned to the left).
If I sum the spill range in B6 using =SUM(B1#) it shows the result 0 while if I sum B1+B2 I get a correct result.
Question 1: What's the explanation of the incorrect sum result in B6 versus the correct one in B7?
Question 2: Is there a different way to display a zero as blank, but keep the ability to calculate the range containing that value?
Question 1:
The TEXT function returns text and computers can't add text strings together. They can only add numbers together.
Excel will sometimes automatically perform type conversions to allow formulas to deliver what Excel guesses as being the expected result. In this case, it is sometimes turning text into numbers before adding them together. This can make like easier for users but the inconsistency can easily lead to errors.
Say A1 and A2 are formatted as text and contain the text character "1". Excel will do an implicit type conversion for operators (+-*/):
=A1 + A2
It won't do conversion for ranges in functions:
=SUM(A1:A2)
=SUM(A1,A2)
When taking in a range, the SUM function will ignore all text, so that it can still sum the numbers in the range without throwing an error. If all cells in the range contain text it will return 0.
However if you tried to use the addition operator on two cells containing text that can't be converted to numbers, it will throw an error.
Note that when you put "A1 + A2" inside the SUM function, excel first evaluates the addition operation (as this is a single input within the function which must be evaluated first), so it converts A1 and A2 to numbers at this point to create a single numeric result, and then the SUM function takes just the single numeric value as input and returns it again as the total.
If you use SUM with two separate inputs, as =SUM(A1,A2), it doesn't convert either input to a number first.
Question 2:
To get the correct result using the SUM function over the range, you can modify the original sequence formula so that it is delivering numeric values. This can be done in various ways:
1 - Convert the text back to a number by multiplying by 1 (forcing another implicit type conversion), handling the error generated for the nullstring which can't be converted to a number:
=IFERROR(TEXT(SEQUENCE(4,,-2),"0;-0;")*1,"")
2 - Test for 0 using an IF statement and return the null string:
=IF(SEQUENCE(4,,-2)<>0,SEQUENCE(4,,-2),"")
3 - Invert the sequence twice, which throws an error only when it is equal to 0:
=IFERROR(1/(1/SEQUENCE(4,,-2)),"")
OR you can modify the SUM formula to convert the range to numbers on input:
=SUM(IFERROR(B1#*1,0))
However this approach requires you to modify all formulas that look at the original sequence. If the original sequence is intended to be used as numbers (as it is in this case), it is better to have it be generated as numbers in the first place.
Is it possible to format numbers in scientific notation as exponents with the base 10 rather than E in Excel?
For example, Excel converts 0.00123 to 1.23E3, but I would like the format
This is not possible by using the standard number formatting in Excel.
You could of course convert your number to a text string with =TEXT(A1;"0.00E+00"), and then you can convert this text string to a new text string in the format that you want. It gets a bit tricky with the superscripts, as you have to display these with the UNICHAR function (this function is available since Excel 2013).
The Unicode values to use with the function are the following (from superscript 0 till 9):
8304,185,178,179,8308,8309,8310,8311,8312,8313
So superscript 50 would be =UNICHAR(8309)&UNICHAR(8304)
You can create elsewhere in your workbook a named range with this values, so the conversion would be easy with an INDEX.
Actually, I would create two ranges, one for the first digit of the exponent (where you don't have a 0, but an empty string, as you don't want to display 10^03 but 10^3), and the second one for the second digit of the exponent (where you keep the 0).
To summarize:
Convert the number to a text sting with =TEXT(A1;"0.00E+00")
Replace the "E" with "x"
Replace the "+" with "10"
If number is negative, add a -
Replace the two last characters with the corresponding superscript (convert back to number with NUMBERVALUE() and then use INDEX() to get the Unicode value to feed into the UNICHAR function.
So for the last digit, the formula is (the named range for the Unicode values is EXPO1 and EXPO2 in my case):
=UNICHAR(INDEX(EXPO2;1;NUMBERVALUE(RIGHT(A1;1))+1))
For the first digit of the exponent (i.e. the one but last character of the text string), use this formula to convert into superscript:
=UNICHAR(INDEX(EXPO1;1;NUMBERVALUE(LEFT(RIGHT(A1;2);1)+1)))
I am a bit lazy here, I could use MID as well instead of the LEFT/RIGHT combination. And please note that the named range is now EXPO1, where the first value is the Unicode value of the empty string (8203).
The entire formula is:
=LEFT(TEXT(A1;"0.00E+00");4)&
"x10"&
UNICHAR(INDEX(EXPO1;1;NUMBERVALUE(LEFT(RIGHT(TEXT(A1;"0.00E+00");2);1)+1)))&
UNICHAR(INDEX(EXPO2;1;NUMBERVALUE(RIGHT(TEXT(A1;"0.00E+00");1))+1))
with EXPO1 and EXPO2 the named ranges as explained above.
To make the formula completely self-sustained, you can hardcode these into the formula, so this becomes then :
=LEFT(TEXT(A1;"0.00E+00");4)&
"x10"&
UNICHAR(INDEX({8203,185,178,179,8308,8309,8310,8311,8312,8313};1;NUMBERVALUE(LEFT(RIGHT(TEXT(A1;"0.00E+00");2);1)+1)))&
UNICHAR(INDEX({8304,185,178,179,8308,8309,8310,8311,8312,8313};1;NUMBERVALUE(RIGHT(TEXT(A1;"0.00E+00");1))+1))
Finally, please note that the formula is dependent on how you convert the number into text. Per OP initial question, I used 2 digits precision after the comma ("0.00E+00"). If you want to display more, you have to extract more than 4 characters with the first LEFT formula in my example.
You may want to use VBA (Visual Basic for Applications).
I created a VBA macro (https://github.com/hisakatha/exp2superscript) to convert exponents into superscripts via text formatting function.
This macro can be imported in the Excel's Visual Basic for Applications editor and registered in Excel Add-ins, and then the macro can be called in Excel.
Briefly, the macro replaces "E" with "×10" using the VBA Replace function and format the exponent part of numbers as superscripts using an assignment:
<cell>.Characters(Start:=<index of the exponent>).Font.Superscript = True
I'm using the average function excel to get the average of a series of hotel prices in various European cities.
=average(21,42,63,84,105)
I'd like to be able to count the number of variables in each average function (for example, in the above example there's 5). The data is scrubbed from websites which is why it's in the format above rather than placed into separate cells.
Is there a way to do this without taking out the variables, putting them into a cell and then separating out the cells using Text to Columns?
Thanks!
You can turn your equation into a string using FORMULATEXT() and then deduce the number of values being averaged by counting the instances of commas in your string (which relates to your final answer by Total Commas + 1 = Total Values
The first portion of the equation counts the character length with commas. The second portion counts the character length without commas. The difference is simply the number of commas present. We then add one since your last value is not followed by a comma
=LEN(FORMULATEXT(A1))-LEN(SUBSTITUTE(FORMULATEXT(A1),",",""))+1
Assumes your average formula is in cell A1
I am a fan of using UDFs, so here's an alternate method.
You can create a custom User Defined Function (UDF) for this. Just split the function by the deliminator and get your number from the UBOUND().
Public function getNumArgs(inputRng as range) as long
'First check that you are actually looking at a formula
If Left(inputRng.Formula, 1) <> "=" Then
getNumArgs = False
exit function
End If
getnumargs = ubound(split(inputrng.formula, ",")) + 1
end function
You will add 1 because VBA uses Base 0.
You will then use your custom UDF the same way you do any other worksheet formula:
=getNumArgs(A1)
The largest benefit of using a UDF is that you do not have to remember a complex formula.
I have an excel formula that is producing a lot of decimal places and I cannot reduce them using the format cell -> numbers -> decimal places options. Here is the formula.
Cell named V01_MIN
V01_MIN =MIN(6:6)
Has a value of 2
Cell named V01_MAX
V01_MAX =MAX(6:6)
Has a value of 1800
Cell named V01_A
V01_A =1-V01_MIN*V01_B
Has a value of 0.889877642
Cell named V01_B
V01_B =99/(V01_MAX-V01_MIN)
Has a value of 0.055061179
X6=723
X7=V01_A+V01_B*X6 (value of 40.69911012)
X8=1
X9=X7*X8 (value of 40.69911012)
X10=1
X11=X9*X10 (value of 40.69911012)
X13==CONCATENATE(X12,", ",X11)
The final results of X13 are:
V01, 1162, 40.6991101223582
I want them to be:
V01, 1162, 40.7
I'm trying to figure out how to make this happen. I've already tried changing the cell formatting on all of these cells (including the final cell) to one decimal palce and that didn't work.
Cell formatting and the actual number in the cell are two different things.
The cell formatting merely changes how the number is shown to you in the cell.
The actual number in the cell will still keep all precision of the number.
If you wish to have the last number rounded, consider this:
X13=CONCATENATE(X12,", ",ROUND(X11,1))
This will round the result in X11 to 1 decimal place before concatenating.
By concatenating you are changing your data to text instead of a number and the number formats won't effect it. Generally you have two options
Either round within you concatenate function
X13==CONCATENATE(X12,", ",roound(X11,1))
or change it back to a number (easiest way is multiply by 1): Note this won't work in your case since you are joining text strings and variables but is useful to be aware of.
X13==CONCATENATE(X12,", ",X11)*1
and then you can format based on decimal places.
i have some cells d1:d10. Some have numbers, others contain "". The "" is the result of an =iferror(,"") function to leave a blank cell.
I am trying to average d1:d10 but only including the cells that are not "".
I have =AVERAGE(IF(D12:D51<>"",D12:D51)) followed by ctrl+shft+enter but it is still taking the average of all the cells (essentially taking the sum and dividing by 10, where I want it to take the sum and divide by less than 10 depending on the number of "" cells)
I couldn't reproduce your problem in Excel 2013.
Normally, Excel's average function ignores text, empty cells and logical values. So, the following formula should do what you are trying to do.
=AVERAGE(D1:D10)
The if clause in your function returns either some numbers or FALSE. Again, normally, Excel's average function ignores FALSE values so it shouldn't behave like you said. If it somehow is converting boolean values to numeric values based on Excel's version (FALSE to zero) you can just give a string instead of a boolean value so it has to ignore those values:
=AVERAGE(IF(D1:D10<>"", D1:D10, "s"))
Alternatively you can calculate the average without the average function:
=SUM(IF(D1:D10<>"", D1:D10))/COUNT(IF(D1:D10<>"", D1:D10))