forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatter.java
More file actions
25 lines (22 loc) · 803 Bytes
/
Formatter.java
File metadata and controls
25 lines (22 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Formatter {
public String formatAmount(double amount) {
int intValue = (int) amount;
String rublesWordForm;
if (amount - intValue == 0) {
rublesWordForm = getRublesWordForm(intValue);
return intValue + " " + rublesWordForm;
} else {
rublesWordForm = getRublesWordForm(intValue);
return String.format("%.2f", amount) + " " + rublesWordForm;
}
}
private String getRublesWordForm(int rubles) {
if (rubles % 10 == 1 && rubles % 100 != 11) {
return "рубль";
} else if ((rubles % 10 >= 2 && rubles % 10 <= 4) && (rubles % 100 < 10 || rubles % 100 >= 20)) {
return "рубля";
} else {
return "рублей";
}
}
}