2

I am using a third party package called streamlit and one of the APIs can draw a slider in the browser according to this:

streamlit.slider(label, min_value=None, max_value=None, value=None, step=None, format=None, key=None)

The slider that I've created can take on integer values (i.e., 1,000,000 or 10,000, etc)and this function above has an optional argument that can format this integer using printf:

format (str or None) – A printf-style format string controlling how the interface should display numbers. This does not impact the return value. 
Valid formatters: %d %e %f %g %i

For an integer, 1000000, I could set the printf style formatting as format="%d" but I want the output to look like 1,000,000. I can't seem to figure out if there is a way to accomplish this given the available formatting constraints.

slaw
  • 6,591
  • 16
  • 56
  • 109
  • Can you show how are you using this `format` argument? – DarK_FirefoX Mar 30 '20 at 16:03
  • Do you really need to use that third party package for getting the job done? Or are you fine with other formatting approach? – Saimon Mar 30 '20 at 16:07
  • 1
    @Saimon, `streamlit` is not for that. Is a library framework for building Machine Learning and Data Science beautiful apps that can be seen on the browser with instant update on parameter modifications – DarK_FirefoX Mar 30 '20 at 16:09
  • Sorry, printf style formatting doesn't support what you want to do. – martineau Mar 30 '20 at 16:47

2 Answers2

2

If you have big_number = 100000.

In python 3.7 you can do something like this:

print(f"{big_number:,d}")

In older versions:

print('{:,d}'.format(big_number))

And the output will be:

100,000

I am assuming you want this behavior on the range values of the streamlit.slider(). Well, according to streamlit documentation as you stated on your question:

format (str or None) – A printf-style format string controlling how the interface should display numbers. This does not impact the return value. Valid formatters: %d %e %f %g %i

So, it seems it does not support the whole printf-style specification, just those formatters without flags.

Even in the python doc they have to set some flags to achieve this when using str.format():

Note

When formatting a number (int, float, complex, decimal.Decimal and subclasses) with the n type (ex: '{:n}'.format(1234)), the function temporarily sets the LC_CTYPE locale to the LC_NUMERIC locale to decode decimal_point and thousands_sep fields of localeconv() if they are non-ASCII or longer than 1 byte, and the LC_NUMERIC locale is different than the LC_CTYPE locale. This temporary change affects other threads.

This is consistent with this answer

DarK_FirefoX
  • 887
  • 8
  • 20
0

You can use string.format() method, available in python to carry out this task.

Sai Prashanth
  • 144
  • 2
  • 11