Skip to content

Instantly share code, notes, and snippets.

@med-ezzairi
Forked from ismaail/number_to_string.php
Created November 26, 2020 21:49
Show Gist options
  • Save med-ezzairi/e4619c7c6e02931cdfcccee7c65c285b to your computer and use it in GitHub Desktop.
Save med-ezzairi/e4619c7c6e02931cdfcccee7c65c285b to your computer and use it in GitHub Desktop.
Convert Number to Text (FR)
<?php
function number_to_string($number): string
{
$whole = floor($number);
$decimal = bcsub($number, $whole, 2);
$decimalPrefix = (-1 === bccomp($decimal, 0.1, 2)) ? 'zéro ' : '';
$decimal = bcmul($decimal, 100, 2);
$formatter = new NumberFormatter('fr_FR', NumberFormatter::SPELLOUT);
if ('0.00' === $decimal) {
return sprintf('%s Dirhams', $formatter->format($whole));
}
return sprintf(
'%s Dirhams et %s%s Centimes',
$formatter->format($whole),
$decimalPrefix,
$formatter->format($decimal)
);
}
echo number_to_string(1500), "\n"; // mille cinq cents Dirhams
echo number_to_string(1200.99), "\n"; // mille deux cents Dirhams et quatre-vingt-dix-neuf Centimes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment