How do I format 1000000
to 1.000.000
in Python? where the '.' is the decimal-mark thousands separator.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I didn't really understand it; but here is what I understand:
You want to convert 1123000 to 1,123,000. You can do that by using format:
http://docs.python.org/release/3.1.3/whatsnew/3.1.html#pep-378-format-specifier-for-thousands-separator
Example:
Just extending the answer a bit here :)
I needed to both have a thousandth separator and limit the precision of a floating point number.
This can be achieved by using the following format string:
This describes the
format()
-specifier's mini-language:Source: https://www.python.org/dev/peps/pep-0378/#current-version-of-the-mini-language
Strange that nobody mentioned a straightforward solution with regex:
Gives the following output:
It also works if you want to separate the digits only before comma:
gives:
the regex uses lookahead to check that the number of digits after a given position is divisible by 3.
Drawing on the answer by Mikel, I implemented his solution like this in my matplotlib plot. I figured some might find it helpful:
Here's only a alternative answer. You can use split operator in python and through some weird logic Here's the code
Output
An idea