Skip to content

This Ruby library aids one in handling decimals. It is very similar to how SQL decimal data type works, so you can set precision and scale.

License

Notifications You must be signed in to change notification settings

dmgr/dmg_decimal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Decimal data type

Introduction

This library aids one in handling decimals. It is very similar to how SQL decimal data type works. See the example below.

Example

require 'dmg_decimal'

decimal = Dmg::Decimal.real("123.456", :scale => 2)
decimal.to_s('F') #=> "123.46"

decimal = Dmg::Decimal.pips(123456, :scale => 2)
decimal.to_s('F') #=> "1234.56"

decimal = Dmg::Decimal.real(123.456, :precision => 2)
decimal.to_s('F') #=> "120.0"

# some math
(decimal * 2).to_s('F') # => "240.0"

# all options example
%w[round truncate ceil floor].each do |approximation|
  number = 123.456
  
  decimal = Dmg::Decimal.real(number, 
    :scale => 2,
    :scale_approximation => approximation)

  puts "#{ number } approximated to 2 decimal places using #{ approximation } approximation is #{ decimal.to_s('F') }"
end

# 123.456 approximated to 2 decimal places using round approximation is 123.46
# 123.456 approximated to 2 decimal places using truncate approximation is 123.45
# 123.456 approximated to 2 decimal places using ceil approximation is 123.46
# 123.456 approximated to 2 decimal places using floor approximation is 123.45

See a spec file for more examples.

About

This Ruby library aids one in handling decimals. It is very similar to how SQL decimal data type works, so you can set precision and scale.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages