Search code examples
python-3.xsqliteandroid-sqlitedata-conversion

Convert multiple string values (Python 3)


Good Morning All!

This is my first time posting on this site after scouring for advice since I tried my hand at Python.

I'm working on pulling data from a sqlite database and inputting it into an Excel spreadsheet. I've learned how to pull the data, and get it written to a spreadsheet. The issue I'm having though, is one of the values in a table I'm pulling data from presents a dollar amount as a string. For example, $1000.00 is represented by 100000. At first glance it would appear to be $100000, but the application, for whatever reason, stores $1000.00 as 100000.

So my question: Is there a way I can convert these string values into a float, OR into another string to input the dollar sign, and decimal point to show the proper figure? This isn't a conversion for just one value either. It needs to be a blanket conversion, so that whenever I run the script it converts the data no matter what dollar amounts are presented.

So if the table contains values of 50000, 20000, 30000, 5000, 500, I need to be able to convert them into $500.00, $200.00, $300.00, $50.00, $5.00.

I hope that all makes sense and I appreciate any help anyone can give me!


Solution

  • You must divide the value by 100 to strip of the last 2 0s and then use the function printf() to format:

    printf("$%.2f", columnname / 100)
    

    Replace columnname with the name of your column.

    So the result of:

    select printf("$%.2f", 500 / 100)
    

    is:

    $5.00
    

    See the demo.