0

A very simple operation in excel, results in a strange result (too many decimals)

vNuevoValorDeLaCelda=0
TemperaturasPorEscala.Kelvin = vNuevoValorDeLaCelda + 273.15

activesheet.cells(1,1)=TemperaturasPorEscala.Kelvin

in cells(1,1) appears 273,149993896484

I'm trying to make a very simple operation in excel VBA: 0 + 273.15

As result, excel calculates 273.149993896484 in cell.

How I can write exact result (273.15)?

JvdV
  • 70,606
  • 8
  • 39
  • 70
Javier
  • 189
  • 3
  • 16
  • 2
    Declare your number as a variant then use `cDec` to cast your number as a decimal –  Jul 25 '19 at 09:57
  • It's a floating point truncation error. Read this: https://floating-point-gui.de/. TL;DR, computers can't represent numbers to arbitrary precision, and in binary, 273.15 is recurring (100010001.00100110011001100110011001100110011001100110011001100...) so your computer can't represent it without truncating. If you are worried about how it looks in your cell, change the cell formatting to only display 2 decimal places or [use a decimal data type](https://bettersolutions.com/vba/data-types/decimal-data-type.htm) instead of a float. – Dan Jul 25 '19 at 09:59
  • Hi. Thanks in advance for your help formatting cells is quite easy, the problem is that I need exact results, so isn't a valid way. Excel does not calculate with formatted cells – Javier Jul 25 '19 at 10:23
  • It works fine with variant data type and CDec() – Javier Jul 25 '19 at 11:16

0 Answers0