Instantly share code, notes, and snippets.

Embed
What would you like to do?
Utils for converting numbers to Turkish words
public final class TurkishNumberUtils {
private static final String SPACE = " ";
private static final String EMPTY = "";
private static final String[] PERIOD_NAMES = {EMPTY, "bin", "milyon", "milyar", "trilyon", "katrilyon", "kentilyon"};
private static final String[] UNITS_TEXTS = {EMPTY, "bir", "iki", "üç", "dört", "beş", "altı", "yedi", "sekiz", "dokuz"};
private static final String[] TENS_TEXTS = {EMPTY, "on", "yirmi", "otuz", "kırk", "elli", "altmış", "yetmiş", "seksen", "doksan"};
private static final String[] HUNDREDS_TEXTS = {EMPTY, "yüz", "iki yüz", "üç yüz", "dört yüz", "beş yüz", "altı yüz", "yedi yüz", "sekiz yüz", "dokuz yüz"};
private TurkishNumberUtils() {
}
public static String numberToTurkishWords(long number) {
if (number == 0) {
return "sıfır";
}
StringBuilder stringBuilder = new StringBuilder();
for (int periodIndex = 0; periodIndex < PERIOD_NAMES.length; periodIndex++) {
long maxOfPeriod = ((long) Math.pow(1000, periodIndex)) - 1;
if (number > maxOfPeriod) {
stringBuilder.insert(0, SPACE + numberToTurkishWordsPartially(number / (maxOfPeriod + 1), PERIOD_NAMES[periodIndex]));
}
}
return stringBuilder
.toString()
.trim()
.replaceAll("( )+", SPACE);
}
private static String numberToTurkishWordsPartially(long partialValue, String postfix) {
int units = (int) (partialValue % 10);
int tens = (int) ((partialValue / 10) % 10);
int hundreds = (int) ((partialValue / 100) % 10);
if (units + tens + hundreds == 0) {
return EMPTY;
}
String partialResult = HUNDREDS_TEXTS[hundreds] + SPACE + TENS_TEXTS[tens] + SPACE + UNITS_TEXTS[units];
if (postfix.equals(PERIOD_NAMES[1]) && partialResult.trim().equals(UNITS_TEXTS[1])) {
return postfix;
} else {
return partialResult + SPACE + postfix;
}
}
}
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
public class TurkishNumberUtilsTest {
@Test
public void shouldConvertNumberToTurkishWords() {
assertThat(TurkishNumberUtils.numberToTurkishWords(0), equalTo("sıfır"));
assertThat(TurkishNumberUtils.numberToTurkishWords(1), equalTo("bir"));
assertThat(TurkishNumberUtils.numberToTurkishWords(2), equalTo("iki"));
assertThat(TurkishNumberUtils.numberToTurkishWords(3), equalTo("üç"));
assertThat(TurkishNumberUtils.numberToTurkishWords(4), equalTo("dört"));
assertThat(TurkishNumberUtils.numberToTurkishWords(5), equalTo("beş"));
assertThat(TurkishNumberUtils.numberToTurkishWords(6), equalTo("altı"));
assertThat(TurkishNumberUtils.numberToTurkishWords(7), equalTo("yedi"));
assertThat(TurkishNumberUtils.numberToTurkishWords(8), equalTo("sekiz"));
assertThat(TurkishNumberUtils.numberToTurkishWords(9), equalTo("dokuz"));
assertThat(TurkishNumberUtils.numberToTurkishWords(111), equalTo("yüz on bir"));
assertThat(TurkishNumberUtils.numberToTurkishWords(222), equalTo("iki yüz yirmi iki"));
assertThat(TurkishNumberUtils.numberToTurkishWords(333), equalTo("üç yüz otuz üç"));
assertThat(TurkishNumberUtils.numberToTurkishWords(444), equalTo("dört yüz kırk dört"));
assertThat(TurkishNumberUtils.numberToTurkishWords(555), equalTo("beş yüz elli beş"));
assertThat(TurkishNumberUtils.numberToTurkishWords(666), equalTo("altı yüz altmış altı"));
assertThat(TurkishNumberUtils.numberToTurkishWords(777), equalTo("yedi yüz yetmiş yedi"));
assertThat(TurkishNumberUtils.numberToTurkishWords(888), equalTo("sekiz yüz seksen sekiz"));
assertThat(TurkishNumberUtils.numberToTurkishWords(999), equalTo("dokuz yüz doksan dokuz"));
assertThat(TurkishNumberUtils.numberToTurkishWords(10), equalTo("on"));
assertThat(TurkishNumberUtils.numberToTurkishWords(100), equalTo("yüz"));
assertThat(TurkishNumberUtils.numberToTurkishWords(1_000), equalTo("bin"));
assertThat(TurkishNumberUtils.numberToTurkishWords(10_000), equalTo("on bin"));
assertThat(TurkishNumberUtils.numberToTurkishWords(100_000), equalTo("yüz bin"));
assertThat(TurkishNumberUtils.numberToTurkishWords(1_000_000), equalTo("bir milyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(10_000_000), equalTo("on milyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(100_000_000), equalTo("yüz milyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(1_000_000_000), equalTo("bir milyar"));
assertThat(TurkishNumberUtils.numberToTurkishWords(10_000_000_000L), equalTo("on milyar"));
assertThat(TurkishNumberUtils.numberToTurkishWords(100_000_000_000L), equalTo("yüz milyar"));
assertThat(TurkishNumberUtils.numberToTurkishWords(1_000_000_000_000L), equalTo("bir trilyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(10_000_000_000_000L), equalTo("on trilyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(100_000_000_000_000L), equalTo("yüz trilyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(100_000_000_000_000L), equalTo("yüz trilyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(1_000_000_000_000_000L), equalTo("bir katrilyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(10_000_000_000_000_000L), equalTo("on katrilyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(100_000_000_000_000_000L), equalTo("yüz katrilyon"));
assertThat(TurkishNumberUtils.numberToTurkishWords(1_000_000_000_000_000_000L), equalTo("bir kentilyon"));
// Long.MAX_VALUE
assertThat(TurkishNumberUtils.numberToTurkishWords(9_223_372_036_854_775_807L), equalTo("dokuz kentilyon iki yüz yirmi üç katrilyon üç yüz yetmiş iki trilyon otuz altı milyar sekiz yüz elli dört milyon yedi yüz yetmiş beş bin sekiz yüz yedi"));
assertThat(TurkishNumberUtils.numberToTurkishWords(11), equalTo("on bir"));
assertThat(TurkishNumberUtils.numberToTurkishWords(5_730), equalTo("beş bin yedi yüz otuz"));
assertThat(TurkishNumberUtils.numberToTurkishWords(9_000), equalTo("dokuz bin"));
assertThat(TurkishNumberUtils.numberToTurkishWords(11_033), equalTo("on bir bin otuz üç"));
assertThat(TurkishNumberUtils.numberToTurkishWords(907_110), equalTo("dokuz yüz yedi bin yüz on"));
assertThat(TurkishNumberUtils.numberToTurkishWords(123_011_033), equalTo("yüz yirmi üç milyon on bir bin otuz üç"));
assertThat(TurkishNumberUtils.numberToTurkishWords(87_086_000_041L), equalTo("seksen yedi milyar seksen altı milyon kırk bir"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment