BigDecimal в ruby

Hi All.

That there is such a case:

require ‘bigdecimal/math’
include BigMath

num1 = BigDecimal.new(1) / BigDecimal.new(6);
num2= BigDecimal.new(1) / BigDecimal.new(600);
num3 = BigDecimal.new(1) / BigDecimal.new(600000);
num4 = BigDecimal.new(1) / BigDecimal.new(60000011);

printf(num1.to_s + “\n”);
printf(num2.to_s + “\n”);
printf(num3.to_s + “\n”);
printf(num4.to_s + “\n”);

obtain the following results:

0.166666666666666667E0
0.1666666666666667E-2
0.1666666666667E-5
0.16666663611E-7

Question: How I can fixate digits after point (in any manipulation
return 32(for example) digits after point? And the second question is
why it rounds them differently? (first gave to 18 characters, the second
16, third 13, fourth 11)

Thank you in advance.

On 7 Δεκ 2013, at 16:02 , Andrey M. [email protected] wrote:

num4 = BigDecimal.new(1) / BigDecimal.new(60000011);
0.1666666666667E-5
0.16666663611E-7

Question: How I can fixate digits after point (in any manipulation
return 32(for example) digits after point? And the second question is
why it rounds them differently? (first gave to 18 characters, the second
16, third 13, fourth 11)

  1. You can fixate the digits using printf("%f0.5", var); Just like in C.
  2. This is the scientific notation[1]. The numbers are all rounder at 18
    chars.

[1] http://en.wikipedia.org/wiki/Scientific_notation

Thank you in advance.


Posted via http://www.ruby-forum.com/.

Panagiotis (atmosx) Atmatzidis

email: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs