3

Long time learner, first time asker.

Let's pretend I have the variable "Sing" dimensioned as Single, and it's storing the value 5.6

Debug.print Sing displays 5.6, as expected.

When it is stored to the worksheet (Sheet1, A1), it saves "5.59999990463256"

The cell it's going to is formatted as "General".

It works completely fine when stored as a Double.

Any ideas what could be going on here?

I've looked through several posts about Single and decimals, but haven't yet seen anyone who's had this problem.

Sub testing()

    Dim Doub As Double
        Doub = 5.6
    Dim Sing As Single
        Sing = 5.6

    Debug.Print Sing, Doub

    Worksheets("Sheet1").Range("A1").Value = Sing #Pastes 5.59999990463256
    Worksheets("Sheet1").Range("A2").Value = Doub #Pastes 5.6

End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
WirbyKhite
  • 31
  • 2
  • 3
    Welcome to SO! Just to get some background: Do you already know that [5.6 cannot be stored precisely in a floating-point variable](https://stackoverflow.com/q/588004/87698)? The "correct" VBA data type for such values is `Decimal`, which is, unfortunately, [only available as a subtype of Variant](https://stackoverflow.com/q/30917438/87698). – Heinzi May 19 '19 at 20:45
  • Excel stores cell values as Doubles, so it's a conversion/floating point issue – Rory May 19 '19 at 20:56
  • Thanks! I was unaware of the complexity for floating point numbers. Thanks for pointing me the right direction; looks like I'll use the decimal type (or just Double) from now on. – WirbyKhite May 19 '19 at 23:19
  • @WirbyKhite `double` has exactly the same problem as `float`. "Double" stands for "double precision *floating point* number". – GSerg May 20 '19 at 13:35

0 Answers0