Storing and performing calculations on long decimals in Excel without being rounded

帅比萌擦擦* 提交于 2020-11-29 19:10:20

问题


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?


回答1:


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.




回答2:


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




回答3:


You could try to use exponential (Scientific) notation?



来源:https://stackoverflow.com/questions/46719869/storing-and-performing-calculations-on-long-decimals-in-excel-without-being-roun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!