Is there analog of php bcsub function in ruby?

PHP function: php.net/manual/ru/function.bcsub.php

Interested, is there any equivalent in Ruby?
July 9th 19 at 13:07
2 answers
July 9th 19 at 13:09
Solution
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 similar, using 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"
Example php.net:
$a = '1.234';
$b = '5';

echo bcsub($a, $b); // -3
echo bcsub($a, $b, 4); // -3.7660


I have the same out a different picture using round:
(1.234 - 5).round # -4
 (1.234 - 5).round(4) # -3.766
- alfonzo.Zeml commented on July 9th 19 at 13:12
although 4-hell match. But why then the first line diverge? - alfonzo.Zeml commented on July 9th 19 at 13:15
Ahhh. Understood. Have php by default, this is perceived as a number with no floating point. Right? - alfonzo.Zeml commented on July 9th 19 at 13:18
Is rounded up to 3 for some reason, although a clear preponderance in favor of 4-Ki. - alfonzo.Zeml commented on July 9th 19 at 13:21
: updated the answer, maybe it will suit You - Deon_Stamm66 commented on July 9th 19 at 13:24
July 9th 19 at 13:11
And can poke his nose, please? - alfonzo.Zeml commented on July 9th 19 at 13:14

Find more questions by tags PHPRuby