com.jk.util.NumbersToWords Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jk-util Show documentation
Show all versions of jk-util Show documentation
This is utility classes used by my other projects.
The newest version!
/*
* Copyright 2002-2016 Jalal Kiswani.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jk.util;
import java.text.DecimalFormat;
/**
* The Class NumbersToWords.
*
* @author Jalal Kiswani
*/
public class NumbersToWords {
private static final String[] tensNames = { "", " Ten", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety" };
private static final String[] numNames = { "", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine", " Ten", " Eleven",
" Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen" };
/**
* Convert.
*
* @param number
* the number
* @return the string
*/
public static String convert(final long number) {
// 0 to 999 999 999 999
if (number == 0) {
return "zero";
}
String snumber = Long.toString(number);
// pad with "0"
final String mask = "000000000000";
final DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number);
// XXXnnnnnnnnn
final int billions = Integer.parseInt(snumber.substring(0, 3));
// nnnXXXnnnnnn
final int millions = Integer.parseInt(snumber.substring(3, 6));
// nnnnnnXXXnnn
final int hundredThousands = Integer.parseInt(snumber.substring(6, 9));
// nnnnnnnnnXXX
final int thousands = Integer.parseInt(snumber.substring(9, 12));
String tradBillions;
switch (billions) {
case 0:
tradBillions = "";
break;
case 1:
tradBillions = convertLessThanOneThousand(billions) + " Billion ";
break;
default:
tradBillions = convertLessThanOneThousand(billions) + " Billion ";
}
String result = tradBillions;
String tradMillions;
switch (millions) {
case 0:
tradMillions = "";
break;
case 1:
tradMillions = convertLessThanOneThousand(millions) + " Million ";
break;
default:
tradMillions = convertLessThanOneThousand(millions) + " Million ";
}
result = result + tradMillions;
String tradHundredThousands;
switch (hundredThousands) {
case 0:
tradHundredThousands = "";
break;
case 1:
tradHundredThousands = "One Thousand ";
break;
default:
tradHundredThousands = convertLessThanOneThousand(hundredThousands) + " Thousand ";
}
result = result + tradHundredThousands;
String tradThousand;
tradThousand = convertLessThanOneThousand(thousands);
result = result + tradThousand;
// remove extra spaces!
final String replaceAll = result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
return replaceAll.trim();
// return replaceAll.replace(" ", "_");
}
private static String convertLessThanOneThousand(int number) {
String soFar;
if (number % 100 < 20) {
soFar = numNames[number % 100];
number /= 100;
} else {
soFar = numNames[number % 10];
number /= 10;
soFar = tensNames[number % 10] + soFar;
number /= 10;
}
if (number == 0) {
return soFar;
}
return numNames[number] + " Hundred" + soFar;
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(final String[] args) {
System.err.println(NumbersToWords.convert(45233));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy