Safely convert dollars to cents
up vote
3
down vote
favorite
For my project, I need to convert dollar to cents, so I wrote the following function to safely do the conversion. The input dollar is float
and the output should be int
.
def safe_dollar_to_cent(dollar):
parts = str(dollar).split('.')
msg = 'success'
status = 0
if len(parts) == 1:
return status, int(parts[0])*100, msg
decimal_part = parts[1]
if len(decimal_part) > 2:
decimal_part = decimal_part[0:2]
msg = 'dollar has been truncated: {} -> {}'.
format(parts[1], decimal_part)
status = 1
ret = int(parts[0]) * 100
multiplier = 10
for i, s in enumerate(decimal_part):
ret += int(s) * multiplier
multiplier /= 10
return status, ret, msg
I am posting here for seeking other some pythonic way of doing this job.
Update:
my input is expected to be float, the return value should be int.
The reason of this implementation is that I found the following incorrect computation.
18.90 * 100 = 1889.9999999999998
python finance
add a comment |
up vote
3
down vote
favorite
For my project, I need to convert dollar to cents, so I wrote the following function to safely do the conversion. The input dollar is float
and the output should be int
.
def safe_dollar_to_cent(dollar):
parts = str(dollar).split('.')
msg = 'success'
status = 0
if len(parts) == 1:
return status, int(parts[0])*100, msg
decimal_part = parts[1]
if len(decimal_part) > 2:
decimal_part = decimal_part[0:2]
msg = 'dollar has been truncated: {} -> {}'.
format(parts[1], decimal_part)
status = 1
ret = int(parts[0]) * 100
multiplier = 10
for i, s in enumerate(decimal_part):
ret += int(s) * multiplier
multiplier /= 10
return status, ret, msg
I am posting here for seeking other some pythonic way of doing this job.
Update:
my input is expected to be float, the return value should be int.
The reason of this implementation is that I found the following incorrect computation.
18.90 * 100 = 1889.9999999999998
python finance
What are you expectingdollar
to be, a number or a string?
– SuperBiasedMan
Feb 25 '16 at 11:56
6
Just to understand what seems overly complicated, what was wrong withreturn int(dollar * 100)
?
– Mathias Ettinger
Feb 25 '16 at 11:58
4
You aren't using fixed-point dollar values? Shame on whoever provided you that interface, floating-point math is anathema for financial stuff. docs.python.org/2/library/decimal.html
– JAB
Feb 25 '16 at 15:47
If you are in total control of your project, consider using cents instead of dollars. Otherwise, consider using Decimal numbers and you w ill have no issue by directly doingint(d * 100.), ((d * 100.) - int(d * 100.)) / 100.
to return a pair having the result and the data you lost
– Luis Masuelli
Feb 25 '16 at 17:53
About your update: you should not use floating point to store money values: what you type as 18.90 is really stored as a binary fractional number, with a little error. But anyway if you use doubles (8 bytes, precision 52 bits), all you need is rounding after the multiply. The error will disappear
– edc65
Feb 25 '16 at 20:25
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
For my project, I need to convert dollar to cents, so I wrote the following function to safely do the conversion. The input dollar is float
and the output should be int
.
def safe_dollar_to_cent(dollar):
parts = str(dollar).split('.')
msg = 'success'
status = 0
if len(parts) == 1:
return status, int(parts[0])*100, msg
decimal_part = parts[1]
if len(decimal_part) > 2:
decimal_part = decimal_part[0:2]
msg = 'dollar has been truncated: {} -> {}'.
format(parts[1], decimal_part)
status = 1
ret = int(parts[0]) * 100
multiplier = 10
for i, s in enumerate(decimal_part):
ret += int(s) * multiplier
multiplier /= 10
return status, ret, msg
I am posting here for seeking other some pythonic way of doing this job.
Update:
my input is expected to be float, the return value should be int.
The reason of this implementation is that I found the following incorrect computation.
18.90 * 100 = 1889.9999999999998
python finance
For my project, I need to convert dollar to cents, so I wrote the following function to safely do the conversion. The input dollar is float
and the output should be int
.
def safe_dollar_to_cent(dollar):
parts = str(dollar).split('.')
msg = 'success'
status = 0
if len(parts) == 1:
return status, int(parts[0])*100, msg
decimal_part = parts[1]
if len(decimal_part) > 2:
decimal_part = decimal_part[0:2]
msg = 'dollar has been truncated: {} -> {}'.
format(parts[1], decimal_part)
status = 1
ret = int(parts[0]) * 100
multiplier = 10
for i, s in enumerate(decimal_part):
ret += int(s) * multiplier
multiplier /= 10
return status, ret, msg
I am posting here for seeking other some pythonic way of doing this job.
Update:
my input is expected to be float, the return value should be int.
The reason of this implementation is that I found the following incorrect computation.
18.90 * 100 = 1889.9999999999998
python finance
python finance
edited Feb 25 '16 at 17:51
asked Feb 25 '16 at 11:42
Alex
11613
11613
What are you expectingdollar
to be, a number or a string?
– SuperBiasedMan
Feb 25 '16 at 11:56
6
Just to understand what seems overly complicated, what was wrong withreturn int(dollar * 100)
?
– Mathias Ettinger
Feb 25 '16 at 11:58
4
You aren't using fixed-point dollar values? Shame on whoever provided you that interface, floating-point math is anathema for financial stuff. docs.python.org/2/library/decimal.html
– JAB
Feb 25 '16 at 15:47
If you are in total control of your project, consider using cents instead of dollars. Otherwise, consider using Decimal numbers and you w ill have no issue by directly doingint(d * 100.), ((d * 100.) - int(d * 100.)) / 100.
to return a pair having the result and the data you lost
– Luis Masuelli
Feb 25 '16 at 17:53
About your update: you should not use floating point to store money values: what you type as 18.90 is really stored as a binary fractional number, with a little error. But anyway if you use doubles (8 bytes, precision 52 bits), all you need is rounding after the multiply. The error will disappear
– edc65
Feb 25 '16 at 20:25
add a comment |
What are you expectingdollar
to be, a number or a string?
– SuperBiasedMan
Feb 25 '16 at 11:56
6
Just to understand what seems overly complicated, what was wrong withreturn int(dollar * 100)
?
– Mathias Ettinger
Feb 25 '16 at 11:58
4
You aren't using fixed-point dollar values? Shame on whoever provided you that interface, floating-point math is anathema for financial stuff. docs.python.org/2/library/decimal.html
– JAB
Feb 25 '16 at 15:47
If you are in total control of your project, consider using cents instead of dollars. Otherwise, consider using Decimal numbers and you w ill have no issue by directly doingint(d * 100.), ((d * 100.) - int(d * 100.)) / 100.
to return a pair having the result and the data you lost
– Luis Masuelli
Feb 25 '16 at 17:53
About your update: you should not use floating point to store money values: what you type as 18.90 is really stored as a binary fractional number, with a little error. But anyway if you use doubles (8 bytes, precision 52 bits), all you need is rounding after the multiply. The error will disappear
– edc65
Feb 25 '16 at 20:25
What are you expecting
dollar
to be, a number or a string?– SuperBiasedMan
Feb 25 '16 at 11:56
What are you expecting
dollar
to be, a number or a string?– SuperBiasedMan
Feb 25 '16 at 11:56
6
6
Just to understand what seems overly complicated, what was wrong with
return int(dollar * 100)
?– Mathias Ettinger
Feb 25 '16 at 11:58
Just to understand what seems overly complicated, what was wrong with
return int(dollar * 100)
?– Mathias Ettinger
Feb 25 '16 at 11:58
4
4
You aren't using fixed-point dollar values? Shame on whoever provided you that interface, floating-point math is anathema for financial stuff. docs.python.org/2/library/decimal.html
– JAB
Feb 25 '16 at 15:47
You aren't using fixed-point dollar values? Shame on whoever provided you that interface, floating-point math is anathema for financial stuff. docs.python.org/2/library/decimal.html
– JAB
Feb 25 '16 at 15:47
If you are in total control of your project, consider using cents instead of dollars. Otherwise, consider using Decimal numbers and you w ill have no issue by directly doing
int(d * 100.), ((d * 100.) - int(d * 100.)) / 100.
to return a pair having the result and the data you lost– Luis Masuelli
Feb 25 '16 at 17:53
If you are in total control of your project, consider using cents instead of dollars. Otherwise, consider using Decimal numbers and you w ill have no issue by directly doing
int(d * 100.), ((d * 100.) - int(d * 100.)) / 100.
to return a pair having the result and the data you lost– Luis Masuelli
Feb 25 '16 at 17:53
About your update: you should not use floating point to store money values: what you type as 18.90 is really stored as a binary fractional number, with a little error. But anyway if you use doubles (8 bytes, precision 52 bits), all you need is rounding after the multiply. The error will disappear
– edc65
Feb 25 '16 at 20:25
About your update: you should not use floating point to store money values: what you type as 18.90 is really stored as a binary fractional number, with a little error. But anyway if you use doubles (8 bytes, precision 52 bits), all you need is rounding after the multiply. The error will disappear
– edc65
Feb 25 '16 at 20:25
add a comment |
3 Answers
3
active
oldest
votes
up vote
10
down vote
I don't like the 3 argument return you're doing. You're using status
and msg
to actually do the same thing. They both return a signal of success unless the dollar is truncated. You don't need both pieces of information. Personally, I'd just say that you only need to print a note about the dollar being truncated so that the function only returns the cents value.
Now, you're also overlooking a very simple formula to convert this:
cents = dollars * 100
You don't need anything more complicated than that for the basic function:
def safe_dollar_to_cent(dollar):
cents = dollar * 100
return cents
If you're not sure that dollar will be a number, you can try converting it:
def safe_dollar_to_cent(dollar):
cents = float(dollar) * 100
return cents
As for truncating, I think it's better as an option. Let the user choose whether or not something will be truncated:
def safe_dollar_to_cent(dollar, truncate=True):
cents = float(dollar) * 100
if truncate:
return int(cents)
else:
return cents
int
will turn cents
into a whole number instead of a decimal, so it's effectively truncating everything after the .
.
1
I think the statements in theif truncate
are swapped. Also, instead of printing a note, maybe usewarnings.warn
?
– Sjoerd Job Postmus
Feb 25 '16 at 15:15
@SjoerdJobPostmus You are correct! Thanks for catching that. You could post a separate answer aboutwarnings.warn
.
– SuperBiasedMan
Feb 25 '16 at 15:40
3
18.90 * 100 = 1889.9999999999998 in my python 2.7.5.
– Alex
Feb 25 '16 at 17:49
add a comment |
up vote
1
down vote
First of all, good job on the solution. It works nicely. Contrary to the other answer here, returning boolean success and message is a industry convention.
What I would suggest is to split the validation and conversion into two functions like this:
def validate_dollar(dollar):
return dollar * 100 % 1 == 0
def safe_dollar_to_cent(dollar):
if validate_dollar(dollar):
return {'success': True, 'cents': int(dollar*100)}
else:
return {'success': False, 'msg': 'Malformed input'}
print(safe_dollar_to_cent(10.9))
print(safe_dollar_to_cent(10))
print(safe_dollar_to_cent(10.90))
print(safe_dollar_to_cent(10.999))
add a comment |
up vote
1
down vote
As stated by OP some values are not directly representable in float thus yielding the nearest value. I can offer two alternatives:
Adding a call to the
round()
function to the proposed solutions of just multiply by 100
int(round(float(dollar)*100))
Just removing the decimal point by editing as a string
("%.2f" % float(amount)).replace('.', '')
New contributor
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
I don't like the 3 argument return you're doing. You're using status
and msg
to actually do the same thing. They both return a signal of success unless the dollar is truncated. You don't need both pieces of information. Personally, I'd just say that you only need to print a note about the dollar being truncated so that the function only returns the cents value.
Now, you're also overlooking a very simple formula to convert this:
cents = dollars * 100
You don't need anything more complicated than that for the basic function:
def safe_dollar_to_cent(dollar):
cents = dollar * 100
return cents
If you're not sure that dollar will be a number, you can try converting it:
def safe_dollar_to_cent(dollar):
cents = float(dollar) * 100
return cents
As for truncating, I think it's better as an option. Let the user choose whether or not something will be truncated:
def safe_dollar_to_cent(dollar, truncate=True):
cents = float(dollar) * 100
if truncate:
return int(cents)
else:
return cents
int
will turn cents
into a whole number instead of a decimal, so it's effectively truncating everything after the .
.
1
I think the statements in theif truncate
are swapped. Also, instead of printing a note, maybe usewarnings.warn
?
– Sjoerd Job Postmus
Feb 25 '16 at 15:15
@SjoerdJobPostmus You are correct! Thanks for catching that. You could post a separate answer aboutwarnings.warn
.
– SuperBiasedMan
Feb 25 '16 at 15:40
3
18.90 * 100 = 1889.9999999999998 in my python 2.7.5.
– Alex
Feb 25 '16 at 17:49
add a comment |
up vote
10
down vote
I don't like the 3 argument return you're doing. You're using status
and msg
to actually do the same thing. They both return a signal of success unless the dollar is truncated. You don't need both pieces of information. Personally, I'd just say that you only need to print a note about the dollar being truncated so that the function only returns the cents value.
Now, you're also overlooking a very simple formula to convert this:
cents = dollars * 100
You don't need anything more complicated than that for the basic function:
def safe_dollar_to_cent(dollar):
cents = dollar * 100
return cents
If you're not sure that dollar will be a number, you can try converting it:
def safe_dollar_to_cent(dollar):
cents = float(dollar) * 100
return cents
As for truncating, I think it's better as an option. Let the user choose whether or not something will be truncated:
def safe_dollar_to_cent(dollar, truncate=True):
cents = float(dollar) * 100
if truncate:
return int(cents)
else:
return cents
int
will turn cents
into a whole number instead of a decimal, so it's effectively truncating everything after the .
.
1
I think the statements in theif truncate
are swapped. Also, instead of printing a note, maybe usewarnings.warn
?
– Sjoerd Job Postmus
Feb 25 '16 at 15:15
@SjoerdJobPostmus You are correct! Thanks for catching that. You could post a separate answer aboutwarnings.warn
.
– SuperBiasedMan
Feb 25 '16 at 15:40
3
18.90 * 100 = 1889.9999999999998 in my python 2.7.5.
– Alex
Feb 25 '16 at 17:49
add a comment |
up vote
10
down vote
up vote
10
down vote
I don't like the 3 argument return you're doing. You're using status
and msg
to actually do the same thing. They both return a signal of success unless the dollar is truncated. You don't need both pieces of information. Personally, I'd just say that you only need to print a note about the dollar being truncated so that the function only returns the cents value.
Now, you're also overlooking a very simple formula to convert this:
cents = dollars * 100
You don't need anything more complicated than that for the basic function:
def safe_dollar_to_cent(dollar):
cents = dollar * 100
return cents
If you're not sure that dollar will be a number, you can try converting it:
def safe_dollar_to_cent(dollar):
cents = float(dollar) * 100
return cents
As for truncating, I think it's better as an option. Let the user choose whether or not something will be truncated:
def safe_dollar_to_cent(dollar, truncate=True):
cents = float(dollar) * 100
if truncate:
return int(cents)
else:
return cents
int
will turn cents
into a whole number instead of a decimal, so it's effectively truncating everything after the .
.
I don't like the 3 argument return you're doing. You're using status
and msg
to actually do the same thing. They both return a signal of success unless the dollar is truncated. You don't need both pieces of information. Personally, I'd just say that you only need to print a note about the dollar being truncated so that the function only returns the cents value.
Now, you're also overlooking a very simple formula to convert this:
cents = dollars * 100
You don't need anything more complicated than that for the basic function:
def safe_dollar_to_cent(dollar):
cents = dollar * 100
return cents
If you're not sure that dollar will be a number, you can try converting it:
def safe_dollar_to_cent(dollar):
cents = float(dollar) * 100
return cents
As for truncating, I think it's better as an option. Let the user choose whether or not something will be truncated:
def safe_dollar_to_cent(dollar, truncate=True):
cents = float(dollar) * 100
if truncate:
return int(cents)
else:
return cents
int
will turn cents
into a whole number instead of a decimal, so it's effectively truncating everything after the .
.
edited Feb 25 '16 at 15:39
answered Feb 25 '16 at 12:06
SuperBiasedMan
11.8k52660
11.8k52660
1
I think the statements in theif truncate
are swapped. Also, instead of printing a note, maybe usewarnings.warn
?
– Sjoerd Job Postmus
Feb 25 '16 at 15:15
@SjoerdJobPostmus You are correct! Thanks for catching that. You could post a separate answer aboutwarnings.warn
.
– SuperBiasedMan
Feb 25 '16 at 15:40
3
18.90 * 100 = 1889.9999999999998 in my python 2.7.5.
– Alex
Feb 25 '16 at 17:49
add a comment |
1
I think the statements in theif truncate
are swapped. Also, instead of printing a note, maybe usewarnings.warn
?
– Sjoerd Job Postmus
Feb 25 '16 at 15:15
@SjoerdJobPostmus You are correct! Thanks for catching that. You could post a separate answer aboutwarnings.warn
.
– SuperBiasedMan
Feb 25 '16 at 15:40
3
18.90 * 100 = 1889.9999999999998 in my python 2.7.5.
– Alex
Feb 25 '16 at 17:49
1
1
I think the statements in the
if truncate
are swapped. Also, instead of printing a note, maybe use warnings.warn
?– Sjoerd Job Postmus
Feb 25 '16 at 15:15
I think the statements in the
if truncate
are swapped. Also, instead of printing a note, maybe use warnings.warn
?– Sjoerd Job Postmus
Feb 25 '16 at 15:15
@SjoerdJobPostmus You are correct! Thanks for catching that. You could post a separate answer about
warnings.warn
.– SuperBiasedMan
Feb 25 '16 at 15:40
@SjoerdJobPostmus You are correct! Thanks for catching that. You could post a separate answer about
warnings.warn
.– SuperBiasedMan
Feb 25 '16 at 15:40
3
3
18.90 * 100 = 1889.9999999999998 in my python 2.7.5.
– Alex
Feb 25 '16 at 17:49
18.90 * 100 = 1889.9999999999998 in my python 2.7.5.
– Alex
Feb 25 '16 at 17:49
add a comment |
up vote
1
down vote
First of all, good job on the solution. It works nicely. Contrary to the other answer here, returning boolean success and message is a industry convention.
What I would suggest is to split the validation and conversion into two functions like this:
def validate_dollar(dollar):
return dollar * 100 % 1 == 0
def safe_dollar_to_cent(dollar):
if validate_dollar(dollar):
return {'success': True, 'cents': int(dollar*100)}
else:
return {'success': False, 'msg': 'Malformed input'}
print(safe_dollar_to_cent(10.9))
print(safe_dollar_to_cent(10))
print(safe_dollar_to_cent(10.90))
print(safe_dollar_to_cent(10.999))
add a comment |
up vote
1
down vote
First of all, good job on the solution. It works nicely. Contrary to the other answer here, returning boolean success and message is a industry convention.
What I would suggest is to split the validation and conversion into two functions like this:
def validate_dollar(dollar):
return dollar * 100 % 1 == 0
def safe_dollar_to_cent(dollar):
if validate_dollar(dollar):
return {'success': True, 'cents': int(dollar*100)}
else:
return {'success': False, 'msg': 'Malformed input'}
print(safe_dollar_to_cent(10.9))
print(safe_dollar_to_cent(10))
print(safe_dollar_to_cent(10.90))
print(safe_dollar_to_cent(10.999))
add a comment |
up vote
1
down vote
up vote
1
down vote
First of all, good job on the solution. It works nicely. Contrary to the other answer here, returning boolean success and message is a industry convention.
What I would suggest is to split the validation and conversion into two functions like this:
def validate_dollar(dollar):
return dollar * 100 % 1 == 0
def safe_dollar_to_cent(dollar):
if validate_dollar(dollar):
return {'success': True, 'cents': int(dollar*100)}
else:
return {'success': False, 'msg': 'Malformed input'}
print(safe_dollar_to_cent(10.9))
print(safe_dollar_to_cent(10))
print(safe_dollar_to_cent(10.90))
print(safe_dollar_to_cent(10.999))
First of all, good job on the solution. It works nicely. Contrary to the other answer here, returning boolean success and message is a industry convention.
What I would suggest is to split the validation and conversion into two functions like this:
def validate_dollar(dollar):
return dollar * 100 % 1 == 0
def safe_dollar_to_cent(dollar):
if validate_dollar(dollar):
return {'success': True, 'cents': int(dollar*100)}
else:
return {'success': False, 'msg': 'Malformed input'}
print(safe_dollar_to_cent(10.9))
print(safe_dollar_to_cent(10))
print(safe_dollar_to_cent(10.90))
print(safe_dollar_to_cent(10.999))
answered Feb 25 '16 at 18:55
Martin Gottweis
1112
1112
add a comment |
add a comment |
up vote
1
down vote
As stated by OP some values are not directly representable in float thus yielding the nearest value. I can offer two alternatives:
Adding a call to the
round()
function to the proposed solutions of just multiply by 100
int(round(float(dollar)*100))
Just removing the decimal point by editing as a string
("%.2f" % float(amount)).replace('.', '')
New contributor
add a comment |
up vote
1
down vote
As stated by OP some values are not directly representable in float thus yielding the nearest value. I can offer two alternatives:
Adding a call to the
round()
function to the proposed solutions of just multiply by 100
int(round(float(dollar)*100))
Just removing the decimal point by editing as a string
("%.2f" % float(amount)).replace('.', '')
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
As stated by OP some values are not directly representable in float thus yielding the nearest value. I can offer two alternatives:
Adding a call to the
round()
function to the proposed solutions of just multiply by 100
int(round(float(dollar)*100))
Just removing the decimal point by editing as a string
("%.2f" % float(amount)).replace('.', '')
New contributor
As stated by OP some values are not directly representable in float thus yielding the nearest value. I can offer two alternatives:
Adding a call to the
round()
function to the proposed solutions of just multiply by 100
int(round(float(dollar)*100))
Just removing the decimal point by editing as a string
("%.2f" % float(amount)).replace('.', '')
New contributor
edited Nov 13 at 16:11
Sᴀᴍ Onᴇᴌᴀ
7,67561748
7,67561748
New contributor
answered Nov 13 at 13:44
badc0de
111
111
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f121074%2fsafely-convert-dollars-to-cents%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are you expecting
dollar
to be, a number or a string?– SuperBiasedMan
Feb 25 '16 at 11:56
6
Just to understand what seems overly complicated, what was wrong with
return int(dollar * 100)
?– Mathias Ettinger
Feb 25 '16 at 11:58
4
You aren't using fixed-point dollar values? Shame on whoever provided you that interface, floating-point math is anathema for financial stuff. docs.python.org/2/library/decimal.html
– JAB
Feb 25 '16 at 15:47
If you are in total control of your project, consider using cents instead of dollars. Otherwise, consider using Decimal numbers and you w ill have no issue by directly doing
int(d * 100.), ((d * 100.) - int(d * 100.)) / 100.
to return a pair having the result and the data you lost– Luis Masuelli
Feb 25 '16 at 17:53
About your update: you should not use floating point to store money values: what you type as 18.90 is really stored as a binary fractional number, with a little error. But anyway if you use doubles (8 bytes, precision 52 bits), all you need is rounding after the multiply. The error will disappear
– edc65
Feb 25 '16 at 20:25