streamlit

How to display numbers with Portuguese formatting in a Streamlit dashboard?


I am building a Streamlit dashboard where the final audience speaks Portuguese. In Portuguese, the decimal and thousands separators are different from English:

Number: 1000.5

English format: 1,000.5

Portuguese format: 1.000,5

By default, Python and Streamlit use the English format, but I need to adapt the number formatting to follow the Portuguese style (using . for thousands and , for decimals).

What is the best way to achieve this in a Streamlit application? Are there any built-in functions or libraries I should use?

Thanks in advance for any suggestions!


Solution

  • You can use babel for internationalizing Python applications as follows:

    import streamlit as st
    from babel.numbers import format_decimal
    
    number = 1000.5
    formatted_number = format_decimal(number, locale='pt_BR')
    st.write(formatted_number)
    
    # Output
    # 1.000,5
    

    Easy and straightforward.