Python Forum
tip calculator not displaying proper dollar format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tip calculator not displaying proper dollar format
#11
(Sep-01-2017, 02:31 PM)hbknjr Wrote:
(Aug-30-2017, 08:59 AM)Crackity Wrote: I was wondering though if there is a certain situation[s] where one option is preferred over the other?
"{}".format(arg) is for formatting string and printing them. You'll receive a string when using it

whereas round(number,ndigits) is used to round off, it returns float which can further be used for calculations.

Ahh, that explains it! I almost posted it here but thought better of it, but after I finished the tip calculator and moved on to the next project I attempted to make use of both examples to verify that it worked the way I thought it would... and it didn't!

However your explanation... explains it! Smile

On my next project I was supposed to have some user input and some set data, (non iterable??? Blush ) then combine them and provide a grand total. I noticed that when I used the "round" option it printed $1000.0 instead of $1000.00. I went back and just used the "format" option instead, which seemed to fix it, but now realize that I had them reversed... Or did I?

Sorry just went back and double checked and thought I understood, but now am confused again. If I enter for example:

prep = 1000.00

does Python see that as a string or is it automatically defined as an integer or floating point due to the " . "?

If it still sees it as a string then mystery solved, but if it's already an integer by default then I don't know why the "round" option didn't work for me.

Either way I started this response to say thank you for all of your assistance!

And to Dead_EyE? Vielen Dank for deine hilfe mein freunde.
before you get excited, Ich spreche nur ein bissien Deutche. Big Grin
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#12
(Sep-01-2017, 02:51 PM)DeaD_EyE Wrote: For currencies you can use (is not a must), locale from pythons stdlib.
Which locales you can use, depends on your operation system installed locales.

For example I can't use en_US because I haven't installed it.

import locale

locale.setlocale(locale.LC_ALL, 'de_DE')
eur = locale.currency(12.3391)
print(eur)

locale.setlocale(locale.LC_ALL, 'en_US')
dollar = locale.currency(12.3391)
print(dollar)
It does also round for you. If you take a look into the locale module.
Other modules depends also on locale. For example daytime:

{:%A}'.format(datetime.datetime.now())
Output:
'Freitag'
It's just good to know.

Yeah, awesome, we've Friday.

Hi, thanks for another solution. I follow your instruction to read the locale module doc. I want to try the locale module but I'm not sure if it will break some default settings.
Quote:locale.setlocale(category, locale=None)
If locale is given and not None, setlocale() modifies the locale setting for the category.

Does the locale.setlocale() modify the current operating system language setting? Since I use Chinese, so after I run locale.setlocale(locale.LC_ALL, 'en_US') do I have to run locale.setlocale(locale.LC_ALL, 'zh_CN') ?

(Sep-04-2017, 07:00 AM)Crackity Wrote:
(Sep-01-2017, 02:31 PM)hbknjr Wrote: "{}".format(arg) is for formatting string and printing them. You'll receive a string when using it

whereas round(number,ndigits) is used to round off, it returns float which can further be used for calculations.

Ahh, that explains it! I almost posted it here but thought better of it, but after I finished the tip calculator and moved on to the next project I attempted to make use of both examples to verify that it worked the way I thought it would... and it didn't!

However your explanation... explains it! Smile

On my next project I was supposed to have some user input and some set data, (non iterable??? Blush ) then combine them and provide a grand total. I noticed that when I used the "round" option it printed $1000.0 instead of $1000.00. I went back and just used the "format" option instead, which seemed to fix it, but now realize that I had them reversed... Or did I?

Sorry just went back and double checked and thought I understood, but now am confused again. If I enter for example:

prep = 1000.00

does Python see that as a string or is it automatically defined as an integer or floating point due to the " . "?

If it still sees it as a string then mystery solved, but if it's already an integer by default then I don't know why the "round" option didn't work for me.

Either way I started this response to say thank you for all of your assistance!

And to Dead_EyE? Vielen Dank for deine hilfe mein freunde.
before you get excited, Ich spreche nur ein bissien Deutche. Big Grin 



As far as I know, when you add the dot after the integer, then Python will force it to float. I can't remember where I learn it, maybe Udacity course Introduction to Python.

And I find something that can explain your confusion probably:
Quote:object.__format__(self, format_spec): Called by the format() built-in function (and by extension, the str.format() method of class str) to produce a “formatted” string representation of an object.

I think this means when you call str.format() the __format__() method of the value itself will do the job of formatting the value to produce a “formatted” string representation.
Reply
#13
(Sep-04-2017, 07:00 AM)Crackity Wrote: prep = 1000.00
does Python see that as a string or is it automatically defined as an integer or floating point due to the " . "?

Python sees it as float due to '.'.
prep = '1000.00' , now Python sees it as string due to quotation marks.

You should understand displaying a number is one thing while using for calculation is another.
1000.0000000 is same as 1000.0. Trailing zeroes aren't needed unless you want to display them.
For that matter, you can use. "{:.2f}".format(1000.00)

eg-
>>> prep = 1000.00
>>> print("{:.2f}".format(prep))
1000.00        
>>> print(round(prep,2))
1000.0         # mathematicaly .00 == .0
Reply
#14
Good to know! Thank you so much!!
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#15
Quote:Does the locale.setlocale() modify the current operating system language setting? Since I use Chinese, so after I run locale.setlocale(locale.LC_ALL, 'en_US') do I have to run locale.setlocale(locale.LC_ALL, 'zh_CN') ?

It changes the setting only in the current running program.
Normally we should not set the locale explicit.

When you trying to use i18n, you've to grab the current locale setting from the users environment.
locale.setlocale(locale.LC_ALL, locale.getlocale())
If you just want to output a string for dollar, i18n is overkill.
In this case the normal formatting method is the easiest way to solve the problem quickly.

As suggested before, you can define the decimal places.

fmt = '$ {:.2f}'
value = 1.1337
final_string = fmt.format(value)
print(final_string)

# or shorter
print('$ {:.2f}'.format(value))

# since python 3.6 with format strings
print(f'{value:.2f}')
The curly braces are replaced by the variable. The text after the colon is the format specifier.
Here you get a good overview: https://pyformat.info/

Schöne Grüße aus Deutschland :-)
Maybe i flight this year back to Zibo to my girlfriend :-)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#16
(Sep-04-2017, 10:48 AM)DeaD_EyE Wrote:
Quote:If you just want to output a string for dollar, i18n is overkill.
In this case the normal formatting method is the easiest way to solve the problem quickly.

fmt = '$ {:.2f}'
value = 1.1337
final_string = fmt.format(value)
print(final_string)

# or shorter
print('$ {:.2f}'.format(value))

# since python 3.6 with format strings
print(f'{value:.2f}')
The curly braces are replaced by the variable. The text after the colon is the format specifier.
Here you get a good overview: https://pyformat.info/

Schöne Grüße aus Deutschland :-)
Maybe i flight this year back to Zibo to my girlfriend :-)

Thanks so much for your detailed explanation! Best wishes!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Histogram using pandas dataframe not showing proper output ift38375 1 2,197 Jul-04-2019, 10:43 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020