Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

BigDecimal#to_json

to_json(*args)

Returns a JSON string representing self:

Example
require 'json/add/bigdecimal'
puts BigDecimal(2).to_json
puts BigDecimal(2.0, 4).to_json
puts BigDecimal(Complex(2, 0)).to_json

Output:

Example
{"json_class":"BigDecimal","b":"27:0.2e1"}
{"json_class":"BigDecimal","b":"36:0.2e1"}
{"json_class":"BigDecimal","b":"27:0.2e1"}
See also #to_int #to_r #to_i #to_s also in 4.0 · 3.4 · 3.2 · 3.1 · 3.0