-
-
Save med-ezzairi/e4619c7c6e02931cdfcccee7c65c285b to your computer and use it in GitHub Desktop.
Convert Number to Text (FR)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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