sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

Fraction format gives incorrect results if there is text in the middle

Open snoopyjc opened this issue 5 years ago • 3 comments

Another one from the valid.tsv suite: If a fraction format has text between the integer part and the fraction, then the result integer and denominator is wrong.

Example:

console.log(SSF.format('#"aabded"\\ ??/??', 12.3456789))

gives 10abded 00/81 instead of 12abded 28/81.

snoopyjc avatar Sep 27 '20 21:09 snoopyjc

In this case, this pattern is matching because fmt is set to #??/??:

if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/))) {

then frac is returning [0, 1000, 81] which is being split into 10 and 00.

snoopyjc avatar Sep 27 '20 21:09 snoopyjc

Here is another couple strange cases, allowed by Excel (but not by the documentation):

? "a" / "b" 8
? "a" / "b" ?

snoopyjc avatar Oct 04 '20 12:10 snoopyjc

And just when you thought it can't get any stranger, Excel also suppresses anything after the integer part if the fraction is not needed:

format('# "h" ? "a" / "b" ?', 1.2) -> '1 h 1 a / b 5'
format('# "h" ? "a" / "b" ?', 1)   -> '1            '

snoopyjc avatar Oct 04 '20 12:10 snoopyjc