-8

i have a number like 369900

But the problem is, i need like that 3699,00

Everything i found added comma on 3 digits like 369,900 or adding new zeros to the end. But i just need to add a comma two digits from right.

What i tried is like that:

price = locale.currency(json['minPrice'], symbol=False, grouping=False, international=False)

But that also doesnt help... cause i got that 369900,00 and thats wrong than, cause the decimals are already included in my number

8
  • 1
    What is 369900? A string, an integer, something else? Commented Oct 20, 2023 at 7:59
  • @Codist a string. But i can also convert it to int. So doesnt matter? Just a number where i will add comma two places from right... (thats the cents) Commented Oct 20, 2023 at 8:00
  • What's the meaning of your comma? What's the value of 3699,00? Is it 369900 or 3699? Commented Oct 20, 2023 at 8:03
  • 1
    That doesn't really answer my question... Commented Oct 20, 2023 at 8:05
  • 3
    Put differently: Is your comma like a thousands separator (but with hundreds instead) or is it a decimal separator? Commented Oct 20, 2023 at 8:19

4 Answers 4

2

If the the value is an integer then:

n = 369900

s = f'{n//100},{n%100:02d}'

print(s)

If it's a string then:

n = '369900'

s = f'{n[:-2]},{n[-2:]}'

print(s)
Sign up to request clarification or add additional context in comments.

Comments

0

If you have a string, just splice the comma in between the last two characters:

>>> s = "369900"
>>> s[:-2] + "," + s[-2:]
'3699,00'
>>>

Comments

0

If you want to do it using locale, you need to convert the string to an integer, and then make sure you set a localization which uses commas. You'll also need to convert the string to number type, and divide by 100 to get the format you're expecting.

The localization string varies by platform, so make sure you check yours. As an example, using windows with German localization:

import locale

locale.setlocale(locale.LC_ALL, 'deu_deu')

my_number = '369900'
my_number = float(my_number)/100
formatted_number = locale.currency(my_number, symbol=False, grouping=False, international=False)

Output:

>'3699,00'

see this question regarding localizations: What is the correct way to set Python's locale on Windows?

Comments

-1

I'm assuming you are looking fora specific locale of currency formatting. You should probably take a look at babel.

The following lines should give you the result:

from babel.numbers import format_currency

print(format_currency(369900, 'USD', u',####,#0', locale='en_US', currency_digits=False))

5 Comments

Have you tested your code? "3699,00.00" isn't the desired format.
@gre_gor: Sure, if the decimals were not needed, then there is a way to do that as well. Do take a look at the updated line of code please.
This is outputting "12,3699,00" for 12369900, which isn't right either.
right. my intent was to show what could be used and how instead of posting the exact snippet. The output format could be modified through the 3rd param, which stands for format. That's the link to the documentation: link
You should be posting the right 3rd param. Other will be just copy pasting your code. A solution with a hidden flaw is bad solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.