Skip to content

Commit 7ae6a39

Browse files
committed
#11 adicionando exemplos em frances para NumberFormat
1 parent 58652e8 commit 7ae6a39

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

book/03-localization/sections/04-formats.asc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ include::{section-java-package}/formats/NumberFormat_NumberToString.java[tag=cod
4444
----
4545
pt_BR: 1.000,05
4646
en_US: 1,000.05
47+
fr_FR: 1 000,05
4748
----
4849
+
4950
Perceba que a representação do número muda de acordo com o `Locale`.
@@ -61,6 +62,7 @@ include::{section-java-package}/formats/NumberFormat_StringToNumber.java[tag=cod
6162
----
6263
pt_BR: 1000.05
6364
en_US: 100005
65+
fr_FR: 1000.05
6466
----
6567
+
6668
Perceba que dependendo do `Locale`, estamos representando um número diferente, e isso muda o resultado do parse.
@@ -78,11 +80,13 @@ include::{section-java-package}/formats/NumberFormat_Currency.java[tag=code]
7880
----
7981
pt_BR: R$ 1.000,05
8082
en_US: $1,000.05
83+
fr_FR: 1 000,05 €
8184
pt_BR: 1000.05
8285
Unparseable number: "R$ 1000,05"
86+
Unparseable number: "R$ 1000,05"
8387
----
8488
+
85-
Perceba que novamente o resultado muda de acordo com o `Locale`. Além disso, não é possível converter a representação da moeda brasileira com um `Locale en_US`.
89+
Perceba que novamente o resultado muda de acordo com o `Locale`. Além disso, não é possível converter a representação da moeda brasileira com um `Locale` `en_US` ou `fr_FR`.
8690

8791
. O `NumberFormat` por ser utilizado para transformar Strings em percentuais, e vice-versa.
8892
+
@@ -97,11 +101,13 @@ include::{section-java-package}/formats/NumberFormat_Percent.java[tag=code]
97101
----
98102
pt_BR: 90%
99103
en_US: 90%
104+
fr_FR: 90 %
100105
pt_BR: 0.8
101106
en_US: 0.8
107+
Unparseable number: "80%"
102108
----
103109
+
104-
Veja que, ao formatar, `100%` é `1`, logo `80%` é `0,8`.
110+
Veja que, ao formatar, `100%` é `1`, logo `80%` é `0,8`. Além disso, no `Locale fr_FR` a representação `80%` não é válida.
105111

106112
. O `NumberFormat` pode ficar complicado ao lidar com vírgulas.
107113
+
@@ -121,6 +127,8 @@ en_US: 8.02
121127
No `Locale pt_BR`, temos o resultado esperado. Porém, no `Locale en_US` o `80,2%` se torna `802%`, pois a vírgula não é usada como separador de decimal.
122128

123129
==== DecimalFormat
130+
131+
124132
==== DateTimeFormatter
125133
==== DateFormat
126134

src/org/j6toj8/localization/formats/NumberFormat_Currency.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,35 @@ public static void main(String[] args) {
1010
// tag::code[]
1111
NumberFormat currencyFormatPtBR = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
1212
NumberFormat currencyFormatEnUS = NumberFormat.getCurrencyInstance(new Locale("en", "US"));
13+
NumberFormat currencyFormatFrFR = NumberFormat.getCurrencyInstance(new Locale("fr", "FR"));
1314

1415
// Valor monetário para String
1516
double d = 1000.05;
1617

1718
System.out.println("pt_BR: " + currencyFormatPtBR.format(d));
1819
System.out.println("en_US: " + currencyFormatEnUS.format(d));
20+
System.out.println("fr_FR: " + currencyFormatFrFR.format(d));
1921

2022
// String para valor Monetário
2123
String s = "R$ 1000,05";
2224

2325
try {
2426
System.out.println("pt_BR: " + currencyFormatPtBR.parse(s));
27+
} catch (ParseException e) {
28+
System.out.println(e.getMessage());
29+
}
30+
31+
try {
2532
System.out.println("en_US: " + currencyFormatEnUS.parse(s));
2633
} catch (ParseException e) {
2734
System.out.println(e.getMessage());
2835
}
36+
37+
try {
38+
System.out.println("fr_FR: " + currencyFormatFrFR.parse(s));
39+
} catch (ParseException e) {
40+
System.out.println(e.getMessage());
41+
}
2942
// end::code[]
3043
}
3144
}

src/org/j6toj8/localization/formats/NumberFormat_NumberToString.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ public static void main(String[] args) {
99
// tag::code[]
1010
NumberFormat numberFormatPtBR = NumberFormat.getInstance(new Locale("pt", "BR"));
1111
NumberFormat numberFormatEnUS = NumberFormat.getInstance(new Locale("en", "US"));
12+
NumberFormat numberFormatFrFR = NumberFormat.getInstance(new Locale("fr", "FR"));
1213

1314
double d = 1000.05;
1415

1516
System.out.println("pt_BR: " + numberFormatPtBR.format(d));
1617
System.out.println("en_US: " + numberFormatEnUS.format(d));
18+
System.out.println("fr_FR: " + numberFormatFrFR.format(d));
1719
// end::code[]
1820
}
1921
}

src/org/j6toj8/localization/formats/NumberFormat_Percent.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ public static void main(String[] args) {
1010
// tag::code[]
1111
NumberFormat percentFormatPtBR = NumberFormat.getPercentInstance(new Locale("pt", "BR"));
1212
NumberFormat percentFormatEnUS = NumberFormat.getPercentInstance(new Locale("en", "US"));
13+
NumberFormat percentFormatFrFR = NumberFormat.getPercentInstance(new Locale("fr", "FR"));
1314

1415
// Percentual para String
1516
double d = 0.9;
1617

1718
System.out.println("pt_BR: " + percentFormatPtBR.format(d));
1819
System.out.println("en_US: " + percentFormatEnUS.format(d));
20+
System.out.println("fr_FR: " + percentFormatFrFR.format(d));
1921

2022
// String para Percentual
2123
String s = "80%";
2224

2325
try {
2426
System.out.println("pt_BR: " + percentFormatPtBR.parse(s));
2527
System.out.println("en_US: " + percentFormatEnUS.parse(s));
28+
System.out.println("fr_FR: " + percentFormatFrFR.parse(s));
2629
} catch (ParseException e) {
27-
// trate a exceção de parse
30+
System.out.println(e.getMessage());
2831
}
2932
// end::code[]
3033
}

src/org/j6toj8/localization/formats/NumberFormat_StringToNumber.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ public static void main(String[] args) {
1010
// tag::code[]
1111
NumberFormat numberFormatPtBR = NumberFormat.getInstance(new Locale("pt", "BR"));
1212
NumberFormat numberFormatEnUS = NumberFormat.getInstance(new Locale("en", "US"));
13+
NumberFormat numberFormatFrFR = NumberFormat.getInstance(new Locale("fr", "FR"));
1314

1415
String s = "1000,05";
1516

1617
try {
1718
Number parsePtBR = numberFormatPtBR.parse(s);
1819
Number parseEnUS = numberFormatEnUS.parse(s);
20+
Number parseFrFR = numberFormatFrFR.parse(s);
1921

2022
System.out.println("pt_BR: " + parsePtBR);
2123
System.out.println("en_US: " + parseEnUS);
24+
System.out.println("fr_FR: " + parseFrFR);
2225
} catch (ParseException e) {
2326
// trate a exceção no parse
2427
}

0 commit comments

Comments
 (0)