dollarFraction¶
Converts a decimal price to a fractional price.
Synopsis¶
dollarFraction (decimalDollar, fraction)
Required Arguments¶
- float
decimalDollar
(Input) - Dollar price expressed as a decimal number.
- int
fraction
(Input) - Denominator of the fractional dollar.
fraction
must be positive.
Return Value¶
The dollar price expressed as a fraction. The numerator is the decimal part of the return value. If no result can be computed, NaN is returned.
Description¶
Function dollarFraction
converts a dollar price, expressed as a decimal
number, into a dollar price, expressed as a fractional price. If no result
can be computed, NaN is returned.
It can be found by solving the following
\[\mathit{idollar} + \frac
{\left[\mathit{decimalDollar} - \mathit{idollar}\right]}
{10^{(\mathrm{ifrac}+1)} / \mathit{fraction}}\]
where idollar is the integer part of the decimalDollar, and ifrac is the integer part of log(fraction).
Example¶
In this example, dollarFraction
converts $1.25 to $1 1/4.
from __future__ import print_function
from numpy import *
from pyimsl.math.dollarFraction import dollarFraction
decimal_dollar = 1.25
fraction = 4
dollarfrc = dollarFraction(decimal_dollar, fraction)
numerator = dollarfrc * 10. - int(dollarfrc) * 10
print("The decimal dollar $1.25 as a fractional dollar = ", end=' ')
print("$%i %i/%i." % (int(dollarfrc), numerator, fraction))
Output¶
The decimal dollar $1.25 as a fractional dollar = $1 1/4.