konsoletyper / teavm

Compiles Java bytecode to JavaScript, WebAssembly and C
https://teavm.org
Apache License 2.0
2.55k stars 261 forks source link

Implement float support for String.format #873

Closed JFronny closed 6 months ago

JFronny commented 7 months ago

The actual implementation is entirely based on TDecimalFormat. The only extra thing is the calculation of the decimal size based on the target width of the string. Additionally, this includes a fix for TDecimalFormat ignoring isGroupingUsed. Probably closes #472

konsoletyper commented 7 months ago

Please, provide tests

JFronny commented 6 months ago

predictDecimalSize is admittedly ugly, but since the width includes other characters and (to my knowledge) DecimalFormat provides no API that accepts a width, it seems needed. Resetting the precision is also not great, but without that, the precision is either wrong or chars get trimmed by formatGivenString. If you want, I could append manually, though.

konsoletyper commented 6 months ago

predictDecimalSize is admittedly ugly, but since the width includes other characters and (to my knowledge) DecimalFormat provides no API that accepts a width, it seems needed.

Yes, that's why I think either TDecimalFormat could be extended compared to original DecimalFormat, or (which I believe a better solution) TDecimalFormat could be just a thin wrapper around internally implemented formatting library, as well as TFormatter. This also would solve the problem with locale-specific plus sign.

konsoletyper commented 6 months ago

I'm ready to merge this PR, however it produces another error in WASI BE. This is not issue of this particular PR, but another issue in WASI BE, that did not uncover it yet. I'll merge this PR as soon as I fix issue in WASI BE.