简体   繁体   English

Excel电子表格中的复杂舍入小数

[英]complex rounding decimals in excel spreadsheet

Have created formula for example =(E2+(G2*37))/290 which returns decimals based on data entered but can't work out how to round the answer to get the following: 已经创建了公式,例如=(E2+(G2*37))/290 ,该公式根据输入的数据返回小数,但无法计算出答案的取整结果:

  1. If 0 to 0.39 round to 0
  2. If 0.4 to 0.89 round to 0.5
  3. If 0.9 to 1.39 round to 1
  4. If 1.4 to 1.89 round to 1.5
  5. If 1.9 to 2.39 round to 2 etc

Hope my question makes sense. 希望我的问题有道理。 Thanks 谢谢

Your custom rounding is simply rounding to the nearest 0.5.....but with an "offset" of 0.15. 您的自定义舍入只是将舍入到最接近的0.5 .....但“偏移”为0.15。 With value to be rounded in A1 you can do that with a simple formula, ie 通过将值四舍五入到A1中,您可以使用一个简单的公式完成该操作

=ROUND(A1*2-0.3,0)/2

or with your calculation in place of A1 that becomes 或用您的计算值代替A1

=ROUND((E2+G2*37)/290*2-0.3,0)/2

It's a bit convoluted but this should work: 这有点令人费解,但这应该可以工作:

=IF(A1 + 0.1 - ROUNDDOWN(A1+0.1;0) < 0.5; ROUNDDOWN(A1+0.1;0); ROUNDDOWN(A1+0.1;0) + 0.5)

where A1 is the cell you want to round. 其中A1是要舍入的单元格。

eg 例如
在此处输入图片说明

NB NB

  • This works only for positive numbers. 这仅适用于正数。
  • Ranges are actually: 范围实际上是:
    [0, 0.39999999...] [0.4 , 0.8999999...] ...
    or equally: 或相等:
    [0, 0.4) [0.4 , 0.9) ...

You could define a VBA function to do this for you: 您可以定义一个VBA函数来为您执行此操作:

Public Function CustomRound(number As Double)
    Dim result As Double, fraction As Double

    fraction = number - Int(number)

    If number <= 0.39 Then
        result = 0
    ElseIf fraction >= 0.4 And fraction <= 0.9 Then
        result = Int(number) + 0.5
    Else
        result = Round(number, 0)
    End If

    CustomRound = result
End Function

You would call this as follows: 您可以这样称呼它:

=CustomRound((E2+(G2*37))/290)

Example: 例:

=CustomRound(0.23) // 0
=CustomRound(1.58) //1.5
=CustomRound(2.12) //2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM