number to words but in French #121405
Unanswered
markussen33
asked this question in
Programming Help
Replies: 1 comment 1 reply
-
I think, this will help you,public static function numberToWordsFrench($number) {
$units = [
0 => '',
1 => 'un',
2 => 'deux',
3 => 'trois',
4 => 'quatre',
5 => 'cinq',
6 => 'six',
7 => 'sept',
8 => 'huit',
9 => 'neuf',
10 => 'dix',
11 => 'onze',
12 => 'douze',
13 => 'treize',
14 => 'quatorze',
15 => 'quinze',
16 => 'seize',
17 => 'dix-sept',
18 => 'dix-huit',
19 => 'dix-neuf',
71 => 'soixante et onze',
72 => 'soixante-douze',
73 => 'soixante-treize',
74 => 'soixante-quatorze',
75 => 'soixante-quinze',
76 => 'soixante-seize',
91 => 'quatre-vingt-onze',
92 => 'quatre-vingt-douze',
93 => 'quatre-vingt-treize',
94 => 'quatre-vingt-quatorze',
95 => 'quatre-vingt-quinze',
96 => 'quatre-vingt-seize'
];
$tens = [
2 => 'vingt',
3 => 'trente',
4 => 'quarante',
5 => 'cinquante',
6 => 'soixante',
7 => 'soixante-dix',
8 => 'quatre-vingts',
9 => 'quatre-vingt-dix'
];
$hundreds = [
1 => 'cent',
2 => 'deux cents',
3 => 'trois cents',
4 => 'quatre cents',
5 => 'cinq cents',
6 => 'six cents',
7 => 'sept cents',
8 => 'huit cents',
9 => 'neuf cents'
];
if ($number == 0) {
return 'zéro';
}
$string = '';
if ($number >= 1000000) {
$string .= self::numberToWordsFrench(floor($number / 1000000)) . ' million ';
$number %= 1000000;
}
if ($number >= 1000) {
if (floor($number / 1000) == 1) {
$string .= 'mille ';
} else {
$string .= self::numberToWordsFrench(floor($number / 1000)) . ' mille ';
}
$number %= 1000;
}
if ($number >= 100) {
// Handle hundreds
$string .= $hundreds[$number / 100] . ' ';
$number %= 100;
}
if ($number >= 20) {
if (($number >= 71 && $number <= 76) || ($number >= 91 && $number <= 96)){
$string .= $units[$number] . ' ';
$number = 0;
} else {
$string .= $tens[floor($number / 10)] . ' ';
$number %= 10;
}
}
if ($number > 0) {
$string .= $units[$number];
}
return trim($string);
} Changes made:
These adjustments should make the output more grammatically correct in French. Feel free to test and let me know if you need further assistance! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Body
Hello fellow developers!
I'd like to start a discussion on implementing a "number to words" functionality in French using PHP. The goal is to create a solution that converts numeric values into their corresponding words in French, for example, converting "123" to "cent vingt-trois".
This feature can be incredibly useful in various applications, such as generating invoices, writing out amounts in French on financial documents, or any scenario where numeric values need to be represented as words in French.
I believe it would be beneficial to discuss different approaches, algorithms , my biggest challange was to test all out put to the if they are grammartically correct , personaly i will share my code feel free to use it or you see an improvment can be done to it :
` public static function numberToWordsFrench($number) {
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions