This is a self-made exercise and may be incorrect. You are welcome to discuss and discuss various optimization and reconstruction solutions.
13-3.
Customizes the class. Write a class to convert the floating point value to the amount. In this exercise, we use the US currency, but readers can also choose any currency.
Basic task (α): compile a dollarize () function, which uses a floating point value as the input and returns the amount in the form of a string. For example:
Dollarize (1234567.8901) => '$1,234,567.89'
The amount returned by dollarize () should contain a comma (such as 1,000,000) and a dollar currency symbol. If there is a negative sign, it must appear on the left side of the dollar sign. After this is done, you can convert it into a useful class named MoneyFmt.
The MoneyFmt class has only one data value (the amount) and five methods (you can write other methods at will ). The _ init _ () constructor initializes the data. The update () method replaces the data value with a new value. _ Nonzero _ () is a Boolean value. If the data value is not zero, True ,__ repr _ () is returned as a floating point value; the _ str _ () method uses the same character format as dollarize () to display the value.
(A) Compile the update () method to implement the data modification function.
(B) Compile the code of the _ str _ () method based on the Code of the dollarize () function you have compiled.
(C) correction example 13.11, moneyfmt. an error in the _ nonzero _ () method in py. This error indicates that all values smaller than 1, for example, 50 cents ($0.50), return a False value ). The main code of the amount Conversion Program (moneyfmt. py) is shown in example 13.11.
(D) Additional question: You can use an optional parameter to specify whether to display a negative number in a pair of angle brackets or a symbol. The default parameter uses a standard negative sign.
Example 13.11 Conversion Program (moneyfmt. py)
The string format class is used to "package" floating point values so that the entire value is displayed as the amount with the correct symbol.
#-*-Encoding: UTF-8-*-class MoneyFmt (object): def _ init _ (self, value = 0.0): # constructor self. value = float (value) def update (self, value = None): # allow updates ##### () cpmplete this function ### def _ repr _ (self): # display as a float return repr (self. value) # The modifications here refer to the English version of the original book def _ str _ (self): # formatted display val = ''###### (B) complete this function... do NOT ### forget about ne Gative numbers !! ### Return val def _ nonzero _ (self): # boolean test ###### (c) find and fix the bug ### return int (self. value)
[Answer]
The (α) function dollarize () code is as follows:
def dollarize(i): roundi = round(i, 2) absi = abs(roundi) if (absi * 10) % 1 == 0: value = format(absi, ',') + '0' else: value = format(absi, ',') if roundi >= 0: return '$' + value else: return '-' + '$' + valueprint dollarize(-1234567.8901)
[Reference]
The application of the Function format comes from here.
Http://www.qqread.com/other-devtool/z476049.html
(A) (B) the code is as follows:
class MoneyFmt(object): def __init__(self, value = 0.0): # constructor self.value = float(value) def update(self, value = None): # allow updates ### (a) Begins if value != None: self.value = float(value) print 'Value has been updated, the new value is: ', self.value else: print 'Value has not been updated.' ### (a) Ends def __repr__(self): # display as a float return repr(self.value) def __str__(self): # formatted display val = '' ### (b) Begins roundi = round(self.value, 2) absi = abs(roundi) if (absi * 10) % 1 == 0: value = format(absi, ',') + '0' else: value = format(absi, ',') if roundi >= 0: val = '$' + value else: val = '-' + '$' + value ### (b) Ends return val def __nonzero__(self): # boolean test ### ### (c) find and fix the bug ### return int(self.value)
[Note]
Save the above Code as the moneyfmt. py file before running. In the Python27 folder, create a folder named PrivateLib and copy the file.
[(A) (B) code running result]
>>> Import sys >>> sys. path. append ('d :\\ python27 \ privatelib ') >>>>> import moneyfmt >>>>>> cash = moneyfmt. moneyFmt (123.45) >>>> cash123.45 >>> print cash $123.45 >>>>> cash. update (100000.4567) Value has been updated, the new value is: 100000.4567 >>>> cash00000.4567 >>> print cash $100,000.46 >>>>> cash. update (-0.3) Value has been updated, the new value is:-0.3 >>>> cash-0.3 >>>> print cash-$0.30 >>> repr (cash )' -0.3 '> 'cash' # This is the only difference from the original book. However, I suspect that the original book is incorrect. 'Cash '>>> str (cash)'-$0.30 '>>>
(D) The Code is as follows:
class MoneyFmt(object): def __init__(self, value = 0.0, flag = '-'): # constructor self.value = float(value) self.flag = flag def update(self, value = None): # allow updates ### (a) Begins if value != None: self.value = float(value) print 'Value has been updated, the new value is: ', self.value else: print 'Value has not been updated.' ### (a) Ends def __repr__(self): # display as a float return repr(self.value) def __str__(self): # formatted display val = '' ### (b) Begins roundi = round(self.value, 2) absi = abs(roundi) if (absi * 10) % 1 == 0: value = format(absi, ',') + '0' else: value = format(absi, ',') if roundi >= 0: val = '$' + value else: val = self.flag + '$' + value ### (b) Ends return val def __nonzero__(self): # boolean test ### ### (c) find and fix the bug ### return int(self.value)
[Note]
Save the above Code as the moneyfmtd. py file before running. In the Python27 folder, create a folder named PrivateLib and copy the file.
[(D) code running result]
>>> import sys>>> sys.path.append('d:\\Python27\\PrivateLib')>>> import moneyfmtd>>> cash1 = moneyfmtd.MoneyFmt(123.45)>>> print cash1$123.45>>> cash2 = moneyfmtd.MoneyFmt(-123.45)>>> print cash2-$123.45>>> cash3 = moneyfmtd.MoneyFmt(-123.45, '<->')>>> print cash3<->$123.45>>> cash3.update(-0.3)Value has been updated, the new value is: -0.3>>> print cash3<->$0.30>>>
[Unfinished]
(C) not completed. No code is displayed in the _ nonzero _ () method. How can I modify the bug? Maybe it's my misunderstanding. Please give me some advice.