zdenekdrahos/bn-php
PHP library for working with Bangla/Bengali numbers and formatting. Convert between Bengali and Latin numerals, handle digit transformations, and support localization-friendly numeric output for apps needing Bangla numeral representation.
Installation Add the package via Composer:
composer require zdenekdrahos/bn-php
No additional configuration is required—it’s a drop-in replacement for PHP’s native float.
First Use Case
Replace float with \BN\BN for precise arithmetic:
use BN\BN;
$a = new BN('1.234567890123456789');
$b = new BN('0.1');
$result = $a->add($b); // No precision loss
echo $result; // Outputs: 1.334567890123456789
Where to Look First
BN.php for core methods (add, sub, mul, div, pow)./tests directory for edge cases (e.g., very large numbers, decimal precision).Precision-Critical Calculations
Use BN for financial, scientific, or high-precision math:
$taxRate = new BN('0.20'); // 20% VAT
$total = new BN('123.456789')->mul($taxRate)->add(new BN('123.456789'));
Database Integration
Store BN as strings in DB (e.g., decimal(38, 20)) and cast on retrieval:
$model->amount = (new BN($request->amount))->toString();
Laravel-Specific Patterns
use BN\BN;
$validator->extend('bn', function ($attribute, $value, $parameters, $validator) {
return (new BN($value))->isFinite();
});
protected $casts = [
'price' => BN::class,
];
Performance Considerations
BN::fromArray() for bulk processing:
$numbers = BN::fromArray(['1.23', '4.56', '7.89']);
$sum = $numbers->reduce(BN::class, 'add');
BN instances where possible (e.g., constants).BN to strings in JSON:
return response()->json(['total' => (string) $bnTotal]);
BN values as strings to avoid precision loss in logs.BN in unit tests to isolate logic:
$this->partialMock(BN::class, ['add'])->willReturn(new BN('100'));
Type Coercion
int/float may lose precision.
$bn = new BN(1.23456789); // Stored as string internally, but constructor accepts floats!
BN as strings:
$bn = new BN('1.234566789'); // Safe
Method Chaining
add() return new BN instances (immutable design).
$result = (new BN('10'))->add('5')->mul('2'); // Works, but creates intermediate objects.
Division Edge Cases
DivisionByZeroException.if ($divisor->equals(new BN('0'))) {
throw new \InvalidArgumentException('Division by zero');
}
Large Number Handling
1e1000) may cause memory issues.BN::gmp() for arbitrary-precision math (requires GMP extension):
$bn = BN::gmp('12345678901234567890');
BN results with PHP’s bcmath for sanity checks:
$bnResult = (new BN('1.1'))->add('2.2');
$bcResult = bcadd('1.1', '2.2', 10);
assert($bnResult->toString() === $bcResult);
(string) $bn or $bn->toString() for debugging—avoid var_dump (shows internal array).BN by creating wrapper classes:
class Money extends BN {
public function format(string $currency = 'USD'): string {
return '$' . number_format((float) $this, 2);
}
}
BN::precision() to use bcmath settings:
BN::setPrecision(bcscale()); // Sync with PHP's bcmath precision
JsonSerializable for custom JSON output:
class SerializableBN extends BN implements \JsonSerializable {
public function jsonSerialize() {
return ['value' => $this->toString(), 'scale' => $this->scale()];
}
}
BN uses 20 decimal places by default (configurable via BN::setPrecision()).1,23 in some regions). Always use . for consistency.How can I help you explore Laravel packages today?