A
A
Anton2016-05-19 14:10:13
ruby
Anton, 2016-05-19 14:10:13

Is there a php equivalent of the bcsub function in ruby?

PHP function: php.net/manual/en/function.bcsub.php
I wonder if there is an analogue in Ruby?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Demidenko, 2016-05-19
@hummingbird

a = 1.234
b = 5
(a - b).round # -4
(a - b).round(1) # -3.8
(a - b).round(2) # -3.77
(a - b).round(3) # -3.766

You can do something similar with sprintf
a = 1.234
b = 5
c = a - b
sprintf("%.f", c) # "-4"
sprintf("%.1f", c) # "-3.8"
sprintf("%.2f", c) # "-3.77"
sprintf("%.3f", c) # "-3.766"
sprintf("%.4f", c) # "-3.7660"

S
Sergey, 2016-05-19
Protko @Fesor

ruby-doc.org/core-2.2.0/Bignum.html#method-i-2D

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question