如何在VBA中四舍五入到小数点后两位? [英] How to round to 2 decimal places in VBA?

查看:196
本文介绍了如何在VBA中四舍五入到小数点后两位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元格B2中,在进行计算之前,我的变量值为297.123.在VBA中,我想将此值舍入为297.12.关于我尝试过的内容,请参见下面的代码.双方都把燃油加到了297.我在做什么错了?

In cell B2 I have variable with the value 297.123 before doing calculations In VBA I would like to round this to 297.12. See the code below as to what I have tried. Both have evaluated fuel to 297. What am I doing wrong?

Dim fuel As Integer
Dim linecount As Long

linecount2 = 2

fuel = Format(ws.Cells(linecount2, 2), "0.00")
fuel = Round(ws.Cells(linecount2, 2), 2)

推荐答案

更改昏暗的燃油->将燃料加倍

您的两个公式都可以使用.

And both of your formula will work.

Dim fuel As Double
Dim linecount2 As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")


linecount2 = 2

fuel = Format(ws.Cells(linecount2, 2), "0.00")
fuel = Round(ws.Cells(linecount2, 2), 2)
fuel = WorksheetFunction.Round(ws.Cells(linecount2, 2), 2) '@suggested by T.M.


MsgBox "try"

End Sub

  • Dim Long 用于整数(整数)
  • Dim Double 用于带小数点的数字(可以存储更多的小数点(最多15位数字)
  • Dim Single 用于带小数点的数字(存储较少的小数点(最多7位数字)
    • Dim Long is for Integers (whole numbers)
    • Dim Double is for numbers with decimal points (can store more decimal points, up to 15 digits)
    • Dim Single is for numbers with decimal points (store less decimal points, up to 7 digits)
    • 这篇关于如何在VBA中四舍五入到小数点后两位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆