-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUAHSpelloutFormat.java
More file actions
33 lines (22 loc) · 1.18 KB
/
UAHSpelloutFormat.java
File metadata and controls
33 lines (22 loc) · 1.18 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
package ua.com.etg.util;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Locale;
import com.ibm.icu.text.RuleBasedNumberFormat;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class UAHSpelloutFormat {
public static String format(BigDecimal money) {
BigDecimal roundedMoney = money.setScale(2, RoundingMode.HALF_UP);
String textMoney = roundedMoney.toPlainString();
int radixLoc = textMoney.indexOf('.');
String kopeck = textMoney.substring(radixLoc + 1, textMoney.length());
RuleBasedNumberFormat numberFormat = new RuleBasedNumberFormat(new Locale("uk"), RuleBasedNumberFormat.SPELLOUT);
numberFormat.setDefaultRuleSet("%spellout-cardinal-feminine");
String formattedIntegerPartOfMoney = numberFormat.format(roundedMoney.intValue());
formattedIntegerPartOfMoney = formattedIntegerPartOfMoney.substring(0, 1).toUpperCase() + formattedIntegerPartOfMoney.substring(1);
String formattedMoney = formattedIntegerPartOfMoney + " грн. " + kopeck + " коп.";
log.debug("{} = {}", money, formattedMoney);
return formattedMoney;
}
}