GithubHelp home page GithubHelp logo

number's Introduction

Converter and object wrapper for a number (float or integer) value

Build Status

<?php
use Granam\Number\NumberObject;
use \Granam\Number\Tools\Exceptions\WrongParameterType;

$stringFloatToNumber = new NumberObject('123.456');
var_dump($stringFloatToNumber->getValue()); // double(123.456)
var_dump((string)$stringFloatToNumber); // string(7) "123.456"

$stringIntToNumber = new NumberObject('123');
var_dump($stringIntToNumber->getValue()); // int(123)
var_dump((string)$stringIntToNumber); // string(7) "123"

$nullToNumber = new NumberObject(null);
var_dump($nullToNumber->getValue()); // int(0)
var_dump((string)$nullToNumber); // string(1) "0"

$tooLongDecimalToNumber = new NumberObject($withTooLongDecimal = '123456.999999999999999999999999999999999999');
var_dump($tooLongDecimalToNumber->getValue());// int(123457); because of intval(floatval($value))

try {
  new NumberObject('123.999999999999999999999999999999', true /* paranoid to rounding */);
} catch (WrongParameterType $floatNumberException) {
  // Something get wrong: Some value has been lost on cast. Given string-number '123456.999999999999999999999999999999999999' results into float 123457
  die('Something get wrong: ' . $floatNumberException->getMessage());
}

number's People

Contributors

jaroslavtyc avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.