io.github.osamabmaq.tafqeetj.converters.NumberToThreeDigitsNumberGroupsDivider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TafqeetJ Show documentation
Show all versions of TafqeetJ Show documentation
A Java library to convert numbers from digital format to words format in Arabic language.
The newest version!
package io.github.osamabmaq.tafqeetj.converters;
import io.github.osamabmaq.tafqeetj.numbers.ThreeDigitsNumber;
import io.github.osamabmaq.tafqeetj.numbers.NumberSign;
import java.util.*;
public class NumberToThreeDigitsNumberGroupsDivider {
private static final List groupsNames = List.of(
"basicNumberGroup", "thousands", "millions", "billions", "trillions"
);
public Map divide(long number) {
List numbersGroups = getThreeDigitsNumberList(
Long.toString(Math.abs(number)),
NumberSign.signOf(number)
);
return mapWithGroupsNames(numbersGroups);
}
private List getThreeDigitsNumberList(String number, NumberSign sign) {
List numbersGroups = new ArrayList<>();
for (int i = number.length(), k = i - 3; i > 0; k = i - 3) {
if (k < 0)
k = 0;
numbersGroups.add(createThreeDigitsNumberOf(number.substring(k, i), sign));
i -= 3;
}
return numbersGroups;
}
private ThreeDigitsNumber createThreeDigitsNumberOf(String number, NumberSign sign) {
return ThreeDigitsNumber.of(Integer.parseInt(number), sign);
}
private Map mapWithGroupsNames(List numbersGroups) {
Map numbersGroupsMap = new HashMap<>();
for (int i = 0; i < numbersGroups.size(); i++)
numbersGroupsMap.put(groupsNames.get(i), numbersGroups.get(i));
return Collections.unmodifiableMap(numbersGroupsMap);
}
}