What is the best method of handling currency/money?
You'll probably want to use a DECIMAL
type in your database. In your migration, do something like this:
# precision is the total number of digits
# scale is the number of digits to the right of the decimal point
add_column :items, :price, :decimal, :precision => 8, :scale => 2
In Rails, the :decimal
type is returned as BigDecimal
, which is great for price calculation.
If you insist on using integers, you will have to manually convert to and from BigDecimal
s everywhere, which will probably just become a pain.
As pointed out by mcl, to print the price, use:
number_to_currency(price, :unit => "€")
#=> €1,234.01
What's the best technique for handling US Dollar calculations in Perl?
See Math::Currency.
Updated:
Assuming all payments adding up to the balance is desirable, I came up with the following script based on the points made by Greg Hewgill:
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw( sum );
my @balances = (10, 1, .50, 5, 7, 12, 3, 2, 8, 1012);
for my $balance (@balances) {
my @stream = get_payment_stream($balance, 3);
my $sum = sum @stream;
print "$balance : @stream : $sum\n";
}
sub get_payment_stream {
my ($balance, $installments) = @_;
$balance *= 100;
my $payment = int($balance / $installments);
$installments -= 1;
my $residual = $balance - int($payment * $installments);
my @stream = (($payment) x $installments, $residual);
return map { sprintf '%.2f', $_ / 100} @stream;
}
Output:
C:\Temp> p
10 : 3.33 3.33 3.34 : 10
1 : 0.33 0.33 0.34 : 1
0.5 : 0.16 0.16 0.18 : 0.5
5 : 1.66 1.66 1.68 : 5
7 : 2.33 2.33 2.34 : 7
12 : 4.00 4.00 4.00 : 12
3 : 1.00 1.00 1.00 : 3
2 : 0.66 0.66 0.68 : 2
8 : 2.66 2.66 2.68 : 8
1012 : 337.33 337.33 337.34 : 1012
Dart: best type to store currency
Definitely do not use double
s for an accounting application. Binary floating point numbers inherently cannot represent exact decimal values, which can lead to inaccurate calculations for seemingly trivial operations. Even though the errors will be small, they can eventually accumulate into larger errors. Setting decimal precision on binary numbers doesn't really make sense.
For currency, you instead either should something intended to store decimal values (e.g. package:decimal
) or should use fixed-point arithmetic to store cents (or whatever the smallest amount of currency is that you want to track). For example, instead of using double
s to store values such as $1.23, use int
s to store amounts in the smallest unit of currency (e.g. 123 cents). Then you can use helper classes to format the amounts wherever they're displayed. For example:
class Money {
int cents;
Money({required this.cents});
@override
String toString() => (cents / 100).toStringAsFixed(2);
Money operator +(Money other) => Money(cents: cents + other.cents);
// Add other operations as desired.
}
Ruby on Rails: how do I format money for my view?
Think this covers it: What is the best method of handling currency/money?.
Which objective-c type is appropriate for handling money?
There are two solutions:
- Use an
int
, and always keep track of monetary values in cents (or the smallest possible division of whatever currency you're using). Use only integer calculations. - Use
NSDecimalNumber
, which does exact decimal arithmetic.
Solution #1 requires you to convert between cents and dollars whenever you do input or output of monetary values, whereas solution #2 can be messier to code (e.g. you have to write something like [num1 decimalNumberByAdding:num2]
instead of num1 + num2
to add two numbers).
I'd recommend solution #1, but go with whichever of those you think would work best.
Ruby on Rails: how do I format money for my view?
Think this covers it: What is the best method of handling currency/money?.
Related Topics
Ruby 2.0.0P0 Irb Warning: "Dl Is Deprecated, Please Use Fiddle"
What's the Difference Between Uri.Escape and Cgi.Escape
How to Update Ruby Version 2.0.0 to the Latest Version in MAC Osx Yosemite
Calling a Method from a String With the Method'S Name in Ruby
Group Hashes by Keys and Sum the Values
Ruby 2.4 and Rails 4 Stack Level Too Deep (Systemstackerror)
I Don't Understand Ruby Local Scope
How to Generate a List of N Unique Random Numbers in Ruby
Ruby Block and Unparenthesized Arguments
What Exactly Is the Singleton Class in Ruby
Ruby: Kind_Of? Vs. Instance_Of? Vs. Is_A
How to Count Duplicate Elements in a Ruby Array
Using Net::Http.Get For an Https Url