Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76904
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <joshua.landau.ws@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'say,': 0.05; 'binary': 0.07; 'defaults': 0.07; 'indicating': 0.07; 'string': 0.09; 'locale': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"good': 0.16; "','": 0.16; "'n'": 0.16; '(something': 0.16; 'arguments:': 0.16; 'comma': 0.16; 'format_spec': 0.16; 'formatted': 0.16; 'function;': 0.16; 'multiplies': 0.16; 'separator,': 0.16; 'separator.': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'adds': 0.24; 'integer': 0.24; 'string,': 0.24; 'math': 0.24; 'cc:2**0': 0.24; 'purposes': 0.26; 'defined': 0.27; 'header:In- Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'words': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'decimal': 0.31; 'though.': 0.31; 'file': 0.32; 'probably': 0.32; 'stuff': 0.32; 'option': 0.32; 'says': 0.33; 'url:python': 0.33; '"the': 0.34; 'subject:with': 0.35; 'received:google.com': 0.35; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'url:library': 0.38; 'how': 0.40; 'number,': 0.60; 'august': 0.61; 'url:3': 0.61; 'first': 0.61; 'places': 0.64; 'answer.': 0.68; '100': 0.79; 'divide': 0.84; 'floors': 0.84; 'front.': 0.84; 'hoist': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=u3ybkAAId41kcMOncA2DuGtCDfTH5QiGSq5YSDXGMzM=; b=RvxDYO3Vu9OBZSEGFSbs/MfsfVcWgpSCy054CbYF++ZO4+BIZ4G0Jhl4LGrUpaggUk mb98YZ5PpJLKNe/QoYqv0/MpXEuzf4W3CkBQgc2btHd046+EqlpukjIzxJk6JEX6BDYb zgttmxDuKF/x193X5ETey5Sjf4+30DLTt/hzw0SyJ1jRS+U762LHMC15ZDQhmbqbMtYC 2CA2BuxGOQ+3tyng6m/XjeiCLbTGTNwjENhHfnyZD7Zw6Zhp2NIFzNvYr+La0VTM7YD4 YWNZbhmBJ48N7jHhP++0c6ZoBAqYvvn7sq1tD9zmKuZ6QHYmV/kLhzGgKNyqsEALuppk GD6A== |
| X-Received | by 10.152.44.230 with SMTP id h6mr11897714lam.51.1408830513162; Sat, 23 Aug 2014 14:48:33 -0700 (PDT) |
| MIME-Version | 1.0 |
| Sender | joshua.landau.ws@gmail.com |
| In-Reply-To | <ek0iv9diq2io1126te9j5pppoilstfuvdo@4ax.com> |
| References | <vrihv9l5sce3bkreceav5uhkaqdo9dqnri@4ax.com> <ek0iv9diq2io1126te9j5pppoilstfuvdo@4ax.com> |
| From | Joshua Landau <joshua@landau.ws> |
| Date | Sat, 23 Aug 2014 22:47:53 +0100 |
| X-Google-Sender-Auth | Ip9QDZIZuCBqkvYk0RjwdFhhf6s |
| Subject | Re: Working with decimals |
| To | Seymore4Head <Seymore4Head@hotmail.invalid> |
| Content-Type | text/plain; charset=UTF-8 |
| Cc | python-list <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13358.1408830907.18130.python-list@python.org> (permalink) |
| Lines | 75 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1408830907 news.xs4all.nl 2854 [2001:888:2000:d::a6]:33370 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:76904 |
Show key headers only | View raw
On 23 August 2014 22:13, Seymore4Head <Seymore4Head@hotmail.invalid> wrote:
> def make_it_money(number):
> import math
> return '
> + str(format(math.floor(number * 100) / 100, ',.2f'))
So for one "import math" should never go inside a function; you should
hoist it to the top of the file with all the other imports.
You then have
def make_it_money(number):
return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))
Consider the
'$' + STUFF
This takes your formatted string (something like '12.43') and adds a
"$" to the front.
So then consider
str(format(math.floor(number * 100) / 100, ',.2f'))
The first thing to note is that format is defined like so:
help(format)
#>>> Help on built-in function format in module builtins:
#>>>
#>>> format(...)
#>>> format(value[, format_spec]) -> string
#>>>
#>>> Returns value.__format__(format_spec)
#>>> format_spec defaults to ""
#>>>
format returns a string, so the str call is unneeded.
You then consider that format takes two arguments:
math.floor(number * 100) / 100
and
',.2f'
Looking at the (well hidden ;P) documentation
(https://docs.python.org/3/library/string.html#formatspec) you find:
"The ',' option signals the use of a comma for a thousands separator.
For a locale aware separator, use the 'n' integer presentation type
instead."
and
"The precision is a decimal number indicating how many digits should
be displayed after the decimal point for a floating point value
formatted with'f' and 'F', or before and after the decimal point for a
floating point value formatted with 'g' or 'G'."
So this says "two decimal places with a comma separator."
Then consider
math.floor(number * 100) / 100
This takes a number, say 12345.6789, multiplies it by 100, to say
1234567.89, floors it, to say 1234567 and then divides by 100, to say,
12345.67.
In other words it floors to two decimal places. The one thing to note
is that binary floating point doesn't divide exactly by 100, so this
might not actually give a perfect answer. It'll probably be "good
enough" for your purposes though.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 13:47 -0400
Re: Working with decimals Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-23 14:21 -0400
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 15:07 -0400
Re: Working with decimals Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-23 15:22 -0400
Re: Working with decimals Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-23 20:24 +0100
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 15:48 -0400
Re: Working with decimals Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-23 21:31 +0100
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 17:13 -0400
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 22:47 +0100
Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-24 08:31 +1000
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 23:47 +0100
Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-24 08:53 +1000
Re: Working with decimals Larry Hudson <orgnut@yahoo.com> - 2014-08-24 00:04 -0700
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 10:58 -0400
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 11:12 -0400
Re: Working with decimals Larry Hudson <orgnut@yahoo.com> - 2014-08-24 14:24 -0700
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 19:07 -0400
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:12 +0100
Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:17 -0600
Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:19 -0600
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:25 +0100
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:29 +0100
Re: Working with decimals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-25 12:16 +1000
Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-25 12:27 +1000
Re: Working with decimals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-25 12:51 +1000
Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-25 13:01 +1000
Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:37 -0600
Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:40 -0600
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:49 +0100
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 22:52 +0100
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 18:03 -0400
csiph-web