Skip to content

Instantly share code, notes, and snippets.

@ismaail
Created November 7, 2019 21:48
Show Gist options
  • Save ismaail/97fac731149d2d02a204a55a61998f81 to your computer and use it in GitHub Desktop.
Save ismaail/97fac731149d2d02a204a55a61998f81 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