Bonjour,
je cherche un code qui convertie mes chiffre en lettre par exemple :merci.Code:655 >>six cent cinquante-cinq
Version imprimable
Bonjour,
je cherche un code qui convertie mes chiffre en lettre par exemple :merci.Code:655 >>six cent cinquante-cinq
Code:
1
2
3 $fmt = new NumberFormatter('fr_FR', NumberFormatter::SPELLOUT); echo $fmt->format(655); //six-cent-cinquante-cinq
Merci,
Mais j'ai l'erreur :je travaille avec php5.2.6Code:Fatal error: Class 'NumberFormatter' not found
Un petit clic jusque sur la doc : http://fr.php.net/manual/fr/numberformatter.create.php
Donc PHP 5.3.0 minimum
Donc pour la version 5.2.6 existe t'il une solution ?
solution est :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 <?php function int2str($a){ $joakim = explode('.',$a); if (isset($joakim[1]) && $joakim[1]!=''){ return int2str($joakim[0]).' virgule '.int2str($joakim[1]) ; } if ($a<0) return 'moins '.int2str(-$a); if ($a<17){ switch ($a){ case 0: return 'zero'; case 1: return 'un'; case 2: return 'deux'; case 3: return 'trois'; case 4: return 'quatre'; case 5: return 'cinq'; case 6: return 'six'; case 7: return 'sept'; case 8: return 'huit'; case 9: return 'neuf'; case 10: return 'dix'; case 11: return 'onze'; case 12: return 'douze'; case 13: return 'treize'; case 14: return 'quatorze'; case 15: return 'quinze'; case 16: return 'seize'; } } else if ($a<20){ return 'dix-'.int2str($a-10); } else if ($a<100){ if ($a%10==0){ switch ($a){ case 20: return 'vingt'; case 30: return 'trente'; case 40: return 'quarante'; case 50: return 'cinquante'; case 60: return 'soixante'; case 70: return 'soixante-dix'; case 80: return 'quatre-vingt'; case 90: return 'quatre-vingt-dix'; } } elseif (substr($a, -1)==1){ if( ((int)($a/10)*10)<70 ){ return int2str((int)($a/10)*10).'-et-un'; } elseif ($a==71) { return 'soixante-et-onze'; } elseif ($a==81) { return 'quatre-vingt-un'; } elseif ($a==91) { return 'quatre-vingt-onze'; } } elseif ($a<70){ return int2str($a-$a%10).'-'.int2str($a%10); } elseif ($a<80){ return int2str(60).'-'.int2str($a%20); } else{ return int2str(80).'-'.int2str($a%20); } } else if ($a==100){ return 'cent'; } else if ($a<200){ return int2str(100).' '.int2str($a%100); } else if ($a<1000){ return int2str((int)($a/100)).' '.int2str(100).' '.int2str($a%100); } else if ($a==1000){ return 'mille'; } else if ($a<2000){ return int2str(1000).' '.int2str($a%1000).' '; } else if ($a<1000000){ return int2str((int)($a/1000)).' '.int2str(1000).' '.int2str($a%1000); } } echo int2str("655"); // six-cent-cinquante-cinq
Oui t'a raison.mais avec t'a methode j'obtien :
Code:Fatal error: Class 'NumberFormatter' not found
windows ?? et comment l'ajouter.
j'ai mis le dll dans ext de php puis je l'est activer depuis php.inij'ai redémarrer serveur mais malgré sa le même erreur ??Code:extension=php_intl.dll
il faut aussi mettre les lib ICU qui vont avec, c'est pour sous windows il faut toujours installer les packages officiel
wow tous sa pour une simple opération .en tous cas merci
et pour le code que j'ai poster si j'enléve le cas de zéro le code va bien fonctionner puisque j'aurait pas besoin de zéro le cas de 0.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 <?php function int2str($a){ $joakim = explode('.',$a); if (isset($joakim[1]) && $joakim[1]!=''){ return int2str($joakim[0]).' virgule '.int2str($joakim[1]) ; } if ($a<0) return 'moins '.int2str(-$a); if ($a<17){ switch ($a){ case 0: return ''; case 1: return 'un'; case 2: return 'deux'; case 3: return 'trois'; case 4: return 'quatre'; case 5: return 'cinq'; case 6: return 'six'; case 7: return 'sept'; case 8: return 'huit'; case 9: return 'neuf'; case 10: return 'dix'; case 11: return 'onze'; case 12: return 'douze'; case 13: return 'treize'; case 14: return 'quatorze'; case 15: return 'quinze'; case 16: return 'seize'; } } else if ($a<20){ return 'dix-'.int2str($a-10); } else if ($a<100){ if ($a%10==0){ switch ($a){ case 20: return 'vingt'; case 30: return 'trente'; case 40: return 'quarante'; case 50: return 'cinquante'; case 60: return 'soixante'; case 70: return 'soixante-dix'; case 80: return 'quatre-vingt'; case 90: return 'quatre-vingt-dix'; } } elseif (substr($a, -1)==1){ if( ((int)($a/10)*10)<70 ){ return int2str((int)($a/10)*10).'-et-un'; } elseif ($a==71) { return 'soixante-et-onze'; } elseif ($a==81) { return 'quatre-vingt-un'; } elseif ($a==91) { return 'quatre-vingt-onze'; } } elseif ($a<70){ return int2str($a-$a%10).'-'.int2str($a%10); } elseif ($a<80){ return int2str(60).'-'.int2str($a%20); } else{ return int2str(80).'-'.int2str($a%20); } } else if ($a==100){ return 'cent'; } else if ($a<200){ return int2str(100).' '.int2str($a%100); } else if ($a<1000){ return int2str((int)($a/100)).' '.int2str(100).' '.int2str($a%100); } else if ($a==1000){ return 'mille'; } else if ($a<2000){ return int2str(1000).' '.int2str($a%1000).' '; } else if ($a<1000000){ return int2str((int)($a/1000)).' '.int2str(1000).' '.int2str($a%1000); } } echo int2str("100.32"); // et voilà ce que ca donne ?>
Bonjour,
bravo pour ce code.
Une petite correction :
80 -> quatre-vingts
81-> quatre-vingt-un (pas de s)
82 -> quatre-vingt-deux (pas de s)
-> ...
xxx et un (pas xxx-et-un)
Mais bon. C'est pour chipoter.Code:return int2str((int)($a/10)*10).' et un';
Voila un code qui marche a 100 % pour tous les cas possible :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 <script type="text/javascript"> var res, plus, diz, s, un, mil, mil2, ent, deci, centi, pl, pl2, conj; var t=["","Un","Deux","Trois","Quatre","Cinq","Six","Sept","Huit","Neuf"]; var t2=["Dix","Onze","Douze","Treize","Quatorze","Quinze","Seize","Dix-sept","Dix-huit","Dix-neuf"]; var t3=["","","Vingt","Trente","Quarante","Cinquante","Soixante","Soixante","Quatre-vingt","Quatre-vingt"]; window.onload=calcule function calcule(m){ return trans(m); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // traitement des deux parties du nombre; function decint(n){ switch(n.length){ case 1 : return dix(n); case 2 : return dix(n); case 3 : return cent(n.charAt(0)) + " " + decint(n.substring(1)); default: mil=n.substring(0,n.length-3); if(mil.length<4){ un= (mil==1) ? "" : decint(mil); return un + mille(mil)+ " " + decint(n.substring(mil.length)); } else{ mil2=mil.substring(0,mil.length-3); return decint(mil2) + million(mil2) + " " + decint(n.substring(mil2.length)); } } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // traitement des nombres entre 0 et 99, pour chaque tranche de 3 chiffres; function dix(n){ if(n<10){ return t[parseInt(n)] } else if(n>9 && n<20){ return t2[n.charAt(1)] } else { plus= n.charAt(1)==0 && n.charAt(0)!=7 && n.charAt(0)!=9 ? "" : (n.charAt(1)==1 && n.charAt(0)<8) ? " et " : "-"; diz= n.charAt(0)==7 || n.charAt(0)==9 ? t2[n.charAt(1)] : t[n.charAt(1)]; s= n==80 ? "s" : ""; return t3[n.charAt(0)] + s + plus + diz; } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // traitement des mots "cent", "mille" et "million" function cent(n){ return n>1 ? t[n]+ " Cent" : (n==1) ? " Cent" : ""; } function mille(n){ return n>=1 ? " Mille" : ""; } function million(n){ return n>=1 ? " Millions" : " Million"; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // conversion du nombre function trans(n){ // vérification de la valeur saisie if(!/^\d+[.,]?\d*$/.test(n)){ return "L'expression entrée n'est pas un nombre." } // séparation entier + décimales n=n.replace(/(^0+)|(\.0+$)/g,""); n=n.replace(/([.,]\d{2})\d+/,"$1"); n1=n.replace(/[,.]\d*/,""); n2= n1!=n ? n.replace(/\d*[,.]/,"") : false; // variables de mise en forme ent= !n1 ? "" : decint(n1); deci= !n2 ? "" : decint(n2); if(!n1 && !n2){ return "Entrez une valeur non nulle!" } conj= !n2 || !n1 ? "" : " et "; euro= !n1 ? "" : !/[23456789]00$/.test(n1) ? " Euro": "s Euro"; centi= !n2 ? "" : " centime"; pl= n1>1 ? "s" : ""; pl2= n2>1 ? "s" : ""; // expression complète en toutes lettres return (" " + ent + euro + pl + conj + deci + centi + pl2).replace(/\s+/g," ").replace("cents E","cents E") ; } </script> <?php $a=500.36; ?> <script type="text/javascript"> document.write(calcule("<?php echo $a;?>")); </script>
dans la doc ICU (Intl) ils disent comment ils s'y prenne
http://icu-project.org/apiref/icu4c/...berFormat.htmlCode:
1
2
3
4
5
6
7
8 To see how these rules actually work in practice, consider the following example: Formatting 25,340 with this rule set would work like this: << thousand >> [the rule whose base value is 1,000 is applicable to 25,340] twenty->> thousand >> [25,340 over 1,000 is 25. The rule for 20 applies.] twenty-five thousand >> [25 mod 10 is 5. The rule for 5 is "five." twenty-five thousand << hundred >> [25,340 mod 1,000 is 340. The rule for 100 applies.] twenty-five thousand three hundred >> [340 over 100 is 3. The rule for 3 is "three."] twenty-five thousand three hundred forty [340 mod 100 is 40. The rule for 40 applies. Since 40 divides evenly by 10, the hyphen and substitution in the brackets are omitted.]