summaryrefslogtreecommitdiff
path: root/spec/ruby/library/bigdecimal/to_s_spec.rb
blob: 75741c5050e528c5eccde61d2a4e4c0cfe2d46b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require_relative '../../spec_helper'
require 'bigdecimal'

describe "BigDecimal#to_s" do

  before :each do
    @bigdec_str = "3.14159265358979323846264338327950288419716939937"
    @bigneg_str = "-3.1415926535897932384626433832795028841971693993"
    @bigdec = BigDecimal(@bigdec_str)
    @bigneg = BigDecimal(@bigneg_str)
  end

  it "return type is of class String" do
    @bigdec.to_s.kind_of?(String).should == true
    @bigneg.to_s.kind_of?(String).should == true
  end

  ruby_version_is ''...'2.4' do
    it "the default format looks like 0.xxxxEnn" do
      @bigdec.to_s.should =~ /^0\.[0-9]*E[0-9]*$/
    end
  end

  ruby_version_is '2.4' do
    it "the default format looks like 0.xxxxenn" do
      @bigdec.to_s.should =~ /^0\.[0-9]*e[0-9]*$/
    end
  end

  it "takes an optional argument" do
    lambda {@bigdec.to_s("F")}.should_not raise_error()
  end

  it "starts with + if + is supplied and value is positive" do
    @bigdec.to_s("+").should =~ /^\+.*/
    @bigneg.to_s("+").should_not =~ /^\+.*/
  end

  it "inserts a space every n chars, if integer n is supplied" do
    re =\
      /\A0\.314 159 265 358 979 323 846 264 338 327 950 288 419 716 939 937E1\z/i
    @bigdec.to_s(3).should =~ re

    str1 = '-123.45678 90123 45678 9'
    BigDecimal("-123.45678901234567890").to_s('5F').should ==  str1
    # trailing zeroes removed
    BigDecimal("1.00000000000").to_s('1F').should == "1.0"
    # 0 is treated as no spaces
    BigDecimal("1.2345").to_s('0F').should == "1.2345"
  end

  it "can return a leading space for values > 0" do
    @bigdec.to_s(" F").should =~ /\ .*/
    @bigneg.to_s(" F").should_not =~ /\ .*/
  end

  it "removes trailing spaces in floating point notation" do
    BigDecimal('-123.45678901234567890').to_s('F').should == "-123.4567890123456789"
    BigDecimal('1.2500').to_s('F').should == "1.25"
    BigDecimal('0000.00000').to_s('F').should == "0.0"
    BigDecimal('-00.000010000').to_s('F').should == "-0.00001"
    BigDecimal("5.00000E-2").to_s("F").should == "0.05"

    BigDecimal("500000").to_s("F").should == "500000.0"
    BigDecimal("5E2").to_s("F").should == "500.0"
    BigDecimal("-5E100").to_s("F").should == "-5" + "0" * 100 + ".0"
  end

  it "can use engineering notation" do
    @bigdec.to_s("E").should =~ /^0\.[0-9]*E[0-9]*$/i
  end

  it "can use conventional floating point notation" do
    %w[f F].each do |format_char|
      @bigdec.to_s(format_char).should == @bigdec_str
      @bigneg.to_s(format_char).should == @bigneg_str
      str2 = "+123.45678901 23456789"
      BigDecimal('123.45678901234567890').to_s("+8#{format_char}").should == str2
    end
  end

end