forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriceFormatter.java
More file actions
33 lines (30 loc) · 1.16 KB
/
PriceFormatter.java
File metadata and controls
33 lines (30 loc) · 1.16 KB
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
public class PriceFormatter {
public static String getStringFromDoubleWithToDigitsAfterPoint(double val) {
return String.format("%.2f", val);
}
public static String getCorrectRubStringByDouble(double val) {
int roundedVal = (int) (Math.floor(val));
String strVal = String.format("%d", roundedVal);
String rubString = "рублей";
if (!strVal.endsWith("11")
&& !strVal.endsWith("12")
&& !strVal.endsWith("13")
&& !strVal.endsWith("14")) {
if (strVal.endsWith("1")) {
rubString = "рубль";
} else if (strVal.endsWith("2") ||
strVal.endsWith("3") ||
strVal.endsWith("4")) {
rubString = "рубля";
}
}
return rubString;
}
public static String getFormattedPriceWithRubStringByDouble(double val) {
StringBuilder result = new StringBuilder();
result.append(getStringFromDoubleWithToDigitsAfterPoint(val))
.append(" ")
.append(getCorrectRubStringByDouble(val));
return result.toString();
}
}