reflex-dev / reflex

🕸️ Web apps in pure Python 🐍
https://reflex.dev
Apache License 2.0
19.05k stars 1.08k forks source link

rx.text component does not support formatting floats #2148

Open pedrojfernandez opened 10 months ago

pedrojfernandez commented 10 months ago

I'm trying to format a float as a currency (euros) and I'm using f string for that:

rx.text (f"{AppState.total:.2f}€")

but when the string is generated, if the number has any right decimal equal to zero, they don't appear. For example: float 6, I want to get "6.00€", but I get "6€". Any idea? In a empty python script it works, but not inside this "framework". May be something is happening in between in the rx.text component generating the output.

4nalog commented 10 months ago

Hey team reflex, this seems like a good first issue. Would love to take this up!

picklelo commented 10 months ago

@etaldot sounds good! Just assigned you to it

picklelo commented 10 months ago

I think for this you have to modify the Var.__format__ method here: https://github.com/reflex-dev/reflex/blob/ce47fcfd6cf0f2886c95363b6b84fe3950d07b4e/reflex/vars.py#L279

We will have to read the format_spec and compile it out to the equivalent JS code to format the number

4nalog commented 10 months ago

Hi @picklelo thanks for the directions. I will put up a PR today.

kairess commented 6 months ago

Still doesn't work?

tlifke01 commented 3 months ago

@picklelo @socalledso Any updates on this bug? Seems to still be present in 0.4.9.

curlup commented 2 weeks ago

Up