Coder Social home page Coder Social logo

Comments (4)

jkuchar avatar jkuchar commented on July 21, 2024 2

Strict explicit rounding is probably the most useful feature of this library. 🥇

from math.

BenMorel avatar BenMorel commented on July 21, 2024 1

Hi, this is not a bug, but a feature.

By design, the toBigInteger() method explicitly throws an exception if the current number cannot be converted to a BigInteger without rounding.

Implicicit rounding would create more issues than it would solve.

Your code is not a workaround, it's the proper way to explicitly tell the library that the number needs to be rounded DOWN before converting it!

from math.

ScorpioT1000 avatar ScorpioT1000 commented on July 21, 2024

Workaround is:

BigDecimal::of(microtime(true))
                ->multipliedBy('1000')
                ->toScale(0, RoundingMode::DOWN)    
                ->toBigInteger()

But I think the function toBigInteger must do it by itself

from math.

julian-sawicki-skrepr avatar julian-sawicki-skrepr commented on July 21, 2024

Reproducing

Let's create the decimal 12.5.

$bigDecimal = BigDecimal::zero()->plus(100)->dividedBy(8, 2);
var_dump($bigDecimal);exit;

Output:

object(Brick\Math\BigDecimal)#11269 (2) {
  ["value":"Brick\Math\BigDecimal":private]=>
  string(4) "1250"
  ["scale":"Brick\Math\BigDecimal":private]=>
  int(2)
}

Now let's create the error: Rounding is necessary to represent the result of the operation at this scale..

$bigDecimal = BigDecimal::zero()->plus(100)->dividedBy(8, 2);
$int = $bigDecimal->toInt();
var_dump($int);exit;

Now let's fix the error: Rounding is necessary to represent the result of the operation at this scale. by introducing toScale(0, RoundingMode::HALF_UP).

$bigDecimal = BigDecimal::zero()->plus(100)->dividedBy(8, 2);
$int = $bigDecimal->toScale(0, RoundingMode::HALF_UP)->toInt();
var_dump($int);exit;

Output:

int(13)

from math.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.