-4

How can I convert 150,000.00 to 150,000 in Python.

This was the terminal output

Traceback (most recent call last):
File "D:\\Python Programme\\PC Parts Prise Monitoring System(PPPMS)\\netstar.py", line 12, in \<module\>
print(int(price))
ValueError: invalid literal for int() with base 10: '150,000.00'
Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
  • `int(15000.00)` converts `float` to `int` – gremur Mar 27 '22 at 14:35
  • This answers the question, but afterwards you need to convert float to int: https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python – Natan Mar 27 '22 at 14:37
  • Do you want to convert to the number 150000 or to the string "150,000"? It's unclear. I noticed after answering. – Natan Mar 27 '22 at 14:42
  • In particular the answer to https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python that suggests using locale to handle this is the correct one, especially dealing with monetary amounts. – paisanco Mar 27 '22 at 14:42
  • It would be helpful to provide the code that caused that terminal output – Liam Mar 27 '22 at 14:55

3 Answers3

0

Aassuming that , is used to separate thousands and . separates the decimal places:

My approach would be to remove the "," and cast to float and from float to int (which just removes the decimal places):

def convert_text_to_int(text):
     return int(float(val.replace(',','')))
print(convert_text_to_int(150,000.99)) # 150000

But modules might exist that do it even better for you.

Natan
  • 728
  • 1
  • 7
  • 23
  • 1
    This could get you in trouble if the amount is a monetary amount (150,000 means something different in Germany than the USA for instance). Granted OP didn't specifically say they were working with monetary amounts. Better to use the locale package in such cases. – paisanco Mar 27 '22 at 14:45
0

I looked through your error message, and that explained why you were getting the error.

You're doing print(int(price)), and from your error it's visible that you aren't converting float to int, you are converting a str to int. Normally, even this should work, but your str has a ,.

There are already answers for questions like these, and this is a good one.

You will have to change the code from the above answer to match your locale, so that it converts based on the monetary/currency system you are using.

Now, if you're sure that you simply want to ignore all commas (,), you can use int(float(price.replace(',',''))).

AashvikT
  • 164
  • 9
-1

Similar to the above but without the double datatype casting

def to_int(value:str) -> int:
     # return value.replace(",",'').replace(".","")

    # I simply replace all commas and dots with empty strings
     value = value.replace(",",'')
     return round(float(value)) 

Edited: The commented lines

Tega Ukavwe
  • 99
  • 1
  • 4
  • that way you get 15000000 and not 150000 (so 100 times what he's looking for in this special case) – Natan Mar 27 '22 at 14:58
  • You also need to `.split('.')[0]`, so that everything after `.` is removed. Otherwise, everything after the decimal will stay with the rest of the number. Eg. `'150,000.00'` will turn into `15000000` instead of `150000`. – AashvikT Mar 27 '22 at 15:00