Skip to content

Commit

Permalink
add bigdecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
greenbigfrog committed Feb 5, 2018
1 parent cafec50 commit f1d550b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions spec/pg/numeric_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "../spec_helper"
require "../../src/pg_ext/big_rational"
require "../../src/pg_ext/big_decimal"

private def n(nd, w, s, ds, d)
PG::Numeric.new(nd.to_i16, w.to_i16, s.to_i16, ds.to_i16, d.map(&.to_i16))
Expand All @@ -9,6 +10,10 @@ private def br(n, d)
BigRational.new(n, d)
end

private def bd(d)
BigDecimal.new(d)
end

private def ex(which)
case which
when "nan"
Expand Down Expand Up @@ -100,6 +105,24 @@ describe PG::Numeric do
end
end

it "to_big_d" do
[
{"nan", bd(0)},
{"0", bd(0)},
{"0.0", bd(0)},
{"1", bd(1)},
{"-1", bd(-1)},
{"1.30", bd(1.3)},
{"12345.6789123", bd(12345.6789123)},
{"-0.00009", bd(-0.00009)},
{"-0.000009", bd(-0.000009)},
{"-0.0000009", bd(-0.0000009)},
{"-0.00000009", bd(-0.00000009)},
].each do |x|
ex(x[0]).to_big_d.should eq(x[1])
end
end

it "#to_s" do
[
{"nan", "NaN"},
Expand Down
23 changes: 23 additions & 0 deletions src/pg_ext/big_decimal.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "big"

module PG
struct Numeric
# Returns a BigDecimal representation of the numeric. This retains all precision.
def to_big_d
return BigDecimal.new("0") if nan? || ndigits == 0

# Since BigDecimal allows initialaztion from String, why should one reinvent the wheel?
BigDecimal.new(to_s)
end
end

class ResultSet
def read(t : BigDecimal.class)
read(PG::Numeric).to_big_d
end

def read(t : BigDecimal?.class)
read(PG::Numeric?).try &.to_big_d
end
end
end

0 comments on commit f1d550b

Please sign in to comment.