What Is the Best Method of Handling Currency/Money

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 BigDecimals 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 doubles 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 doubles to store values such as $1.23, use ints 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



Leave a reply



Submit