Posted 8 October 2024, 11:34 pm EST
- Updated 8 October 2024, 11:40 pm EST
Hi,
As per my understanding, you want a formatter that displays up to 20 digits with 10 decimal places, uses a comma as a thousand separator, and removes trailing zeros after the decimal. For example, 12.50 should be displayed as 12.5, and whole numbers like 1200 should be shown as 1,200 without a trailing decimal point (i.e., not as ‘1,200.’).
Regarding the issue of displaying 1200 as ‘1,200.’, this behavior is expected when using this’##,###,###,###,###,###,###.##########’ formatter. Excel also behaves the same way for this formatter.
For the issue of ‘9999999999999999.9999999999’ being displayed as ‘10,000,000,000,000,000’, both edit mode and display mode showing the same value in my end that is ‘10,000,000,000,000,000.’. Refer to the attached gif “Steps.gif.”
Designer: https://developer.mescius.com/spreadjs/designer/index.html
Gif:
If you’re seeing different behavior (e.g., correct value in edit mode but different in display mode), please share a minimal working example with the steps to replicate the issue. This will help me investigate further. Additionally, A GIF or video illustrating the issue would also be helpful.
The reason why ‘9999999999999999.9999999999’ becomes ‘10,000,000,000,000,000’ is due to how JavaScript handles floating-point numbers. JavaScript follows the IEEE 754 standard (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates#numbers), which represents numbers with finite precision (typically 64 bits), allowing for about 15-17 decimal digits of precision.
When inputting 9999999999999999.9999999999, the number exceeds JavaScript’s precise representation, and the nearest representable value becomes 10 quadrillion. This is why parseFloat(“9999999999999999.9999999999”) returns 10,000,000,000,000,000. You can refer to the attached gif “Steps.gif.”
For your specific requirement, you could use custom formatting. Refer this demo to learn more about custom formatting: https://developer.mescius.com/spreadjs/demos/features/cells/formatter/custom-formatter/purejs
Regards,
Priyam