In fact, you’re doing it right already -
There is only "number with two squares after the comma" or in the form of a string. A "number" is a "number" - a quantity: "1", "1.0", "1,000" is the same number. (and even if you use the class "int" to put "1" and "1.0" in a float, Python continues to consider the same as equal: not only does the comparison " 1 == 1.0" be True, but [1.0] used as a key in a dictionary works interchangeably with 1. And this behavior is correct: number-by-number.
So there’s even the "Decimal" numeric type in Python: it can assure you that you’ll have numbers with 2 decimal places in your memory, and you’ll never have a rounding problem that will generate more decimal places. Now, using the float type, as you’re doing - even rounding with the Around or the native Python round, you can’t guarantee that the numbers will remain representable with two decimal places, no more broken values - because internally they’re stored in base 2, and some numbers just turn "periodic dizimas" when you change the basis of representation.
So while your application is running and processing the numbers- you use numbers. If you have any specific need for an algorithm to use only two decimal places, use the "Decimal" type, limiting the number of houses in the context - otherwise, leave the float types without rounding.
In the interfaces of your system with the outside world: either to print in the terminal, to write a file on disk - be it an Excel, a text file, a CSV, or generate an HTML page, with the numbers formatted for display - at this point you apply the "{:02f}". format(number) - You do not have to worry if when making an account the number is "1." or "1.00", "1.00000", because they are the same number. If it is not a numeric account, but needs the number as string ) for example, put it as the monetary representation - this is the "edge" - and you use the string.format at that point.
I saw your comment now - and I noticed that you want to standardize the numbers on a graph - so please add the code that generates the graph - is it with matplotlib? -and then we’ll explore how to format the numbers to be used as Labels in the graph - the above logic remains (it’s probably just passing the string-formatted list in the matplotlib call instead of the numbers as numbers)
In the example that is after you edit the question, returning the numbers in strings formatted with the number of decimals is trivial - if it is always the locale in English - a little more boring, if we have to take into account various locales.
I don’t know where in the format_decimal that you use in your code (the idea was to be able to run here, but without the "import"s, this is not possible). We can use the "format_string" function of the standard Python "locale" library -
And in addition, we will also use a Python syntax called "list comprehension" that reduces a three-line chunk and multiple function calls to just one.
So where have you got:
formatted_numbers = []
# Appending the values formatted on the list created previously
for i in range(len(values)):
formatted_numbers.append(format_decimal(values[i], locale= culture_code_treated))
With list comprhensions it is:
formatted_numbers = [format_decimal(value, locale=culture_code_treated) for value in values]
(Here, one uses the inline "for" - it executes, and executes the expression before it once for each element in the sequence given to the for is (the values variable. Apart from using this syntax, in Python we never need to do for i in range(Len(sequence)): value = sequence[i] ... - ofor in ython always goes through sequences - you don’t need to count the index, for the first thing inside the is to take the element in that index.
Good - but this is the same code that you had - I don’t know yet Which is the "format_decimal" function - and it may not accept the expression for numerical formatting, which we need - so let’s use locale.format_string - which accepts a format string in the syntax using "%" - the first form of Python string interpolation, inherited from the C language prinf. Before calling locale.format_string you have to configure locale, with the set_locale function:
import locale
def formatC(values, culture_code="en-US"):
...
previous_locale = locale.get_locale(locale.LC_NUMERIC)
locale.set_locale(locale.LC_NUMERIC, culture_code.replace("-", "_"))
formatted = [locale.format_string("%.02f", value) for value in values]
locale.set_locale(locale.LC_NUMERIC, previous_locale)
return formatted
In the original question, you had added that function format_decimal
that you are using, comes from the library babel
. I consulted the library documentation (in fact, direct source code, which in the ipython environment can be seen only by placing the function followed by ??
) - and, no, they do not accept an auxiliary form -only convert the numbers without rounding. But the standard library function locale
Python, as I used above, accepts - step by string "%.02f"
for her.
Hi Helena- please don’t do this anymore - I stayed almost an hour explaining an answer to you and you erased the other question. Luckily, I was able to copy the text I wrote. Some people here are boring, and will come with "downvotes" and votes to close, even for perfectly answered questions. I’ll paste my answer back.
– jsbueno
Hi, a thousand apologies! I am new here and did not know that other users could block my questions. I deleted because I thought ngm more could see it. I’m sorry again, and the question is better now?
– Helena Faillace
yeah - that’s good. There are some users who have the role of moderators (I am an old user, with enough reputation, but I have no power of moderation) - from time to time I try to talk to moderators here because I think they have a culture that does not favor new users - I have seen questions that were negative and blocked here have 4 positive votes to be redone in stackoverflow in English.
– jsbueno