hyperformula icon indicating copy to clipboard operation
hyperformula copied to clipboard

[Bug]: Formula evaluation auto-rounds numbers (expected "52.50000", got "52.5")

Open Dhatchanamoorthi8 opened this issue 7 months ago • 6 comments

Description

Hi HyperFormula team 👋,

I'm using HyperFormula.buildFromSheets() to evaluate some sheet data, and I noticed an issue with how numeric results are returned after formula evaluation.

My input (handsontableJson) looks like this: { "Sheet2": [ [ "52.50000", "=ROUND(AVERAGE(A1:A5),5)" ], [ "52.50000", "" ], [ "52.50000", "" ], [ "52.50000", "" ], [ "52.50000", "" ] ] } const hfInstance = HyperFormula.buildFromSheets(handsontableJson, { licenseKey: 'gpl-v3', smartRounding: false, }); const sheetId = hfInstance.getSheetId(sheetKey); const evaluatedData = hfInstance.getSheetValues(sheetId); console.log(evaluatedData); [ [ 52.5, 52.5 ], [ 52.5, "" ], [ 52.5, "" ], [ 52.5, "" ], [ 52.5, "" ] ] Problem:

  • I expected the result to be "52.50000", preserving the 5 decimal places, because the original formula uses =ROUND(AVERAGE(A1:A5),5).
  • But HyperFormula returns it as 52.5, losing the trailing zeros.

Context:

  • smartRounding: false is already set.
  • I would like to retain the precision as formatted (5 decimal places), especially for PDF generation use cases where the exact number format matters.

Question:

  • Is there a built-in way to preserve the decimal formatting (e.g., "52.50000" instead of "52.5")?
  • If not, could this be considered as an enhancement, or is there a recommended workaround?

Video or screenshots

No response

Demo

https://codesandbox.io/p/live/3a6bac78-bfb6-4063-9f95-cfc221f98c37

HyperFormula version

^3.0.0

Your framework

node js

Your environment

Chrome 90

Dhatchanamoorthi8 avatar Apr 29 '25 02:04 Dhatchanamoorthi8

Hi @Dhatchanamoorthi8

Thank you for writing.

Excel will show it as 52.5 unless the cell is formatted to display 5 decimal places, same with HyperFormula. You may, for example, run the result.toFixed(5) to get the zeros. Here's an example https://stackblitz.com/edit/vitejs-vite-83wrkhns?file=index.html,main.js&terminal=dev

AMBudnik avatar Apr 29 '25 07:04 AMBudnik

Hi @AMBudnik ContributorHandle,

Thanks for the clarification!

You're right — toFixed(5) works when we know we always want 5 decimal places. But in my case, the value is dynamic and can come from formulas like =ROUND(AVERAGE(...), N) where N varies. So I’m looking for a way to preserve the number of decimal places based on the formula logic, rather than hardcoding .toFixed().

Is there any way in HyperFormula to:

Respect the formatting precision set in the formula (like ROUND(..., 5)),

Or retrieve the result as a string with the exact decimal places the formula computes?

Right now, getSheetValues() gives me a number like 52.5 even if the formula is =ROUND(..., 5). I want to get 52.50000 without hardcoding toFixed(5) — since the decimal precision might be different for each formula.

Any suggestions or workarounds?

Thanks again!

Dhatchanamoorthi8 avatar Apr 29 '25 08:04 Dhatchanamoorthi8

Could you please give me some more examples as you mentioned that

we always want 5 decimal places

but then

I want to get 52.50000 without hardcoding toFixed(5) — since the decimal precision might be different for each formula.

It seems that those requirements are not aligned. Maybe if you could share a matrix of input < > output data examples I could understand the use case more.

AMBudnik avatar Apr 29 '25 11:04 AMBudnik

hi @AMBudnik My issue is: HyperFormula automatically removes zeros after the decimal point. For example, when the result is 52.50000, it becomes 52.5. But in my case, I want to keep it as 52.50000.

The number of decimal places is different for each formula, so I cannot use .toFixed(5) everywhere. I need HyperFormula to keep the original decimal format based on the formula result.

If needed, I can share some input and expected output examples.

https://codesandbox.io/p/live/3a6bac78-bfb6-4063-9f95-cfc221f98c37

Image

Please refer to the link — it shows that the result returns with trimmed zeros (e.g., 52.5), but in some cases, I need it to return values like 52.50000000. The number of decimal places should change based on my requirement.

Dhatchanamoorthi8 avatar Apr 29 '25 12:04 Dhatchanamoorthi8

Hi @Dhatchanamoorthi8

I see that you also sent us an email, so we will continue there. My colleague or I will reply to you there today.

AMBudnik avatar May 05 '25 07:05 AMBudnik

Hi @AMBudnik,

Thank you for the update. I’ll await your response.

Dhatchanamoorthi8 avatar May 05 '25 11:05 Dhatchanamoorthi8

I'm closing this issue. @Dhatchanamoorthi8 if you have any further questions feel free to re-open it.

sequba avatar Aug 05 '25 09:08 sequba