All Projects → h4kuna → number-format

h4kuna / number-format

Licence: other
Wrapper above number_format and unit convertor.

Programming Languages

PHP
23972 projects - #3 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to number-format

ChangeNumbersJs
Tiny Library for change number from a language in other language.
Stars: ✭ 14 (-22.22%)
Mutual labels:  number-format, number-converter
physikal
Mirror of Gitlab Repository
Stars: ✭ 33 (+83.33%)
Mutual labels:  units
Safe Units
Type-safe TypeScript units of measure 👷📏
Stars: ✭ 137 (+661.11%)
Mutual labels:  units
FormatEditText
格式化输入框,可用来格式化数字、金额、号码等; FormatEditText can be used as a formatted text input box
Stars: ✭ 121 (+572.22%)
Mutual labels:  number-format
Insect
High precision scientific calculator with support for physical units
Stars: ✭ 2,469 (+13616.67%)
Mutual labels:  units
Unchained
A fully type safe, compile time only units library.
Stars: ✭ 70 (+288.89%)
Mutual labels:  units
Openimu
Open Source Analytics & Visualisation Software for Inertial Measurement Units
Stars: ✭ 133 (+638.89%)
Mutual labels:  units
millify
Convert long numbers to pretty, human-readable strings
Stars: ✭ 119 (+561.11%)
Mutual labels:  units
fp-units
An FP-oriented library to easily convert CSS units.
Stars: ✭ 18 (+0%)
Mutual labels:  units
vue-filter-number-format
Vue.js filter for formatting numbers
Stars: ✭ 17 (-5.56%)
Mutual labels:  number-format
Dimensioned
Compile-time dimensional analysis for various unit systems using Rust's type system.
Stars: ✭ 235 (+1205.56%)
Mutual labels:  units
cpc
Text calculator with support for units and conversion
Stars: ✭ 89 (+394.44%)
Mutual labels:  units
prettysize-rs
Pretty-print file sizes and more
Stars: ✭ 29 (+61.11%)
Mutual labels:  units
Unit Api
Units of Measurement API
Stars: ✭ 140 (+677.78%)
Mutual labels:  units
UnitfulAstro.jl
An extension of Unitful.jl for astronomers.
Stars: ✭ 18 (+0%)
Mutual labels:  units
Mathjs
An extensive math library for JavaScript and Node.js
Stars: ✭ 11,861 (+65794.44%)
Mutual labels:  units
python-baseconv
Python module to convert numbers from base 10 integers to base X strings and back again.
Stars: ✭ 40 (+122.22%)
Mutual labels:  number-converter
formatters
A javascript library for formatting and manipulating.
Stars: ✭ 14 (-22.22%)
Mutual labels:  number-format
DustryX
More more content for you
Stars: ✭ 24 (+33.33%)
Mutual labels:  units
measured
Type-safe, intuitive units of measure
Stars: ✭ 81 (+350%)
Mutual labels:  units

Number Format

Build Status Latest stable Downloads this Month Coverage Status

Wrapper above number_format, api is very easy.

Changelog

v3.0

This version is same like v2.0 but support php7.1+.

v2.0

New behavior is representing by one class is one type of format. Onetime create class and you can'nt change by life of object. Added new classes for number, unit and currency. Working with percent and taxes are better too.

Here is manual for older version 1.3.0.

Install via composer

composer require h4kuna/number-format

NumberFormatState

Class has many parameters and all paremetes has default value. You can add parameters normaly by position or name of keys in array like first parameter.

use h4kuna\Number;

// set decimals as 3
$numberFormat = new Number\NumberFormatState(3);
// or
$numberFormat = new Number\NumberFormatState(['decimals' => 3]);


echo $numberFormat->format(1000); // 1 000,000

Parameters

  • decimals: [2]
  • decimalPoint: string [',']
  • thousandsSeparator: string [NULL] mean  
  • zeroIsEmpty: bool [FALSE] - transform 0 to empty value
  • emptyValue: string [NULL] has two options dependecy on zeroIsEmpty if is FALSE than empty value transform to zero or TRUE mean zero tranform to emtpy string if is not defined other string
  • zeroClear: [FALSE] mean 1.20 trim zero from right -> 1.2
  • intOnly: [-1] if we have numbers like integers. This mean set 3 and transform number 1050 -> 1,05
  • round: [0] change round function, let's use NumberFormatState::ROUND_BY_CEIL or NumberFormatState::ROUND_BY_FLOOR

Here is test for more use cases.

UnitFormatState

Use this class for number with unit like Kb, Mb, Gb. Unit symbol is second parameter in method format. Visit tests.

Parameters

  • mask: ['1 U'] mean 1 pattern for number and U is pattern for unit
  • showUnit: [TRUE] mean show unit if number is empty
  • nbsp: [TRUE] mean replace white space in mask by &nbsp

UnitPersistentFormatState

This class is same like previous, but unit is persistent like currencies or temperature.

Parameters

  • unit: has'nt default value

NumberFormatFactory

For all previous classes is prepared factory class. This class help you create new instance and support named parameters in constructor. Visit test

Tax

$tax = new Tax(20);
echo $tax->add(100); // 120
echo $tax->deduct(120); // 100.0
echo $tax->diff(120); // 20.0

Percent

$percent = new Percent(20);
echo $percent->add(100); // 120.0
echo $percent->deduct(120); // 96.0
echo $percent->diff(120); // 24.0

Integration to Nette framework

In your neon file

services:
	number: h4kuna\Number\NumberFormatState(decimalPoint: '.', intOnly: 1, decimals: 1) #support named parameters by nette

	latte.latteFactory:
		setup:
			- addFilter('number', [@number, 'format'])

We added new filter number, in template use like:

{=10000|number} // this render "1 000.0" with &nbps; like white space

Units

Help us convert units in general decimal system.

Units\Unit

use h4kuna\Number\Units;

$unit = new Units\Unit(/* [string $from], [array $allowedUnits] */);
  • $from select default prefix for your units default is BASE = 0
  • $allowedUnits if we need other units if is defined

This example say: I have 50kilo (103) and convert to base 100

$unitValue = $unit->convertFrom(50, $unit::KILO, $unit::BASE);
echo $unitValue->unit; // empty string mean BASE
echo $unitValue->value; // 50000

If second parameter is NULL then use from unit whose is defined in constructor.

$unitValue = $unit->convertFrom(5000, NULL, $unit::KILO);
// alias for this use case is 
$unitValue = $unit->convert(5000, $unit::KILO);

echo $unitValue->unit; // k mean KILO
echo $unitValue->value; // 5

If third parameter is NULL, class try find best unit.

$unitValue = $unit->convertFrom(5000000, $unit::MILI, NULL);
echo $unitValue->unit; // k mean KILO
echo $unitValue->value; // 5

Last method, take string and convert how we need. This is good for Byte.

$unitValue = $unit->fromString('100k', $unit::BASE);
echo $unitValue->unit; // BASE
echo $unitValue->value; // 100000

Units\Byte

$unitValue = $byte = new Units\Byte();
$byte->fromString('128M');
echo $unitValue->unit; // BASE
echo $unitValue->value; // 134217728

Units\UnitFormat

If we need format our units.

$nff = new Number\NumberFormatFactory();
$unitfFormat = new Units\UnitFormat('B', new Byte, $nff->createUnit());

$unitfFormat->convert(968884224); // '924,00 MB'
$unitfFormat->convert(1024); // '1,00 kB'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].