Created
February 12, 2025 03:19
-
-
Save aasmith/d68e70d303e9b02a472ed8edaa4bc7d2 to your computer and use it in GitHub Desktop.
BSD number.c in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 1988, 1993, 1994 | |
* The Regents of the University of California. All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* 1. Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* 2. Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* 3. Neither the name of the University nor the names of its contributors | |
* may be used to endorse or promote products derived from this software | |
* without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
* SUCH DAMAGE. | |
*/ | |
package org.tinnedfruit.number; | |
import java.math.BigDecimal; | |
import java.math.BigInteger; | |
import java.math.RoundingMode; | |
/** | |
* This class is a Java port of the number.c program from the BSD Games package. | |
* The original C code was converted to Java by Gemini for use as a Java library. | |
* The purpose of this class is to convert numeric values into their textual | |
* representations. | |
* <p> | |
* Only this paragraph, the copyright header, and the package name has been written | |
* by a human. The rest of the output is entirely from Gemini 2.0 Flash. | |
* The prompt was a paste of | |
* <a href="https://raw.githubusercontent.com/vattam/BSDGames/04f8d1635706cc7f67fd8c75bccdafe70ee8b084/number/number.c">number.c</a> | |
* with a request to port the code to Java for use as a library. | |
*/ | |
public class NumberConverter { | |
private static final String[] name1 = { | |
"", "one", "two", "three", | |
"four", "five", "six", "seven", | |
"eight", "nine", "ten", "eleven", | |
"twelve", "thirteen", "fourteen", "fifteen", | |
"sixteen", "seventeen", "eighteen", "nineteen", | |
}; | |
private static final String[] name2 = { | |
"", "ten", "twenty", "thirty", | |
"forty", "fifty", "sixty", "seventy", | |
"eighty", "ninety", | |
}; | |
private static final String[] name3 = { | |
"hundred", "thousand", "million", "billion", | |
"trillion", "quadrillion", "quintillion", "sextillion", | |
"septillion", "octillion", "nonillion", "decillion", | |
"undecillion", "duodecillion", "tredecillion", "quattuordecillion", | |
"quindecillion", "sexdecillion", | |
"septendecillion", "octodecillion", | |
"novemdecillion", "vigintillion", | |
}; | |
private boolean lflag; // Instance variable, can be set per instance | |
private static final int MAXNUM = 65; | |
// Constructor to set lflag (for future customization) | |
public NumberConverter(boolean lflag) { | |
this.lflag = lflag; | |
} | |
// Default constructor | |
public NumberConverter() { | |
this(false); // Default lflag to false | |
} | |
public String convert(int number) { | |
return convert(String.valueOf(number)); | |
} | |
public String convert(long number) { | |
return convert(String.valueOf(number)); | |
} | |
public String convert(float number) { | |
return convert(String.valueOf(number)); // Convert to string to handle decimal part | |
} | |
public String convert(double number) { | |
return convert(String.valueOf(number)); // Convert to string to handle decimal part | |
} | |
public String convert(BigDecimal number) { | |
return convert(number.toString()); | |
} | |
public String convert(String line) { | |
StringBuilder result = new StringBuilder(); | |
String fraction = null; | |
int flen = 0; | |
String mainNumber = line.trim(); | |
if (mainNumber.isEmpty()) { | |
return "zero" + (lflag ? "" : "."); | |
} | |
if (mainNumber.contains(".")) { | |
int dotIndex = mainNumber.indexOf("."); | |
fraction = mainNumber.substring(dotIndex + 1); | |
mainNumber = mainNumber.substring(0, dotIndex); | |
} | |
if (mainNumber.startsWith("-")) { | |
result.append("minus").append(lflag ? " " : "\n"); | |
mainNumber = mainNumber.substring(1); | |
} | |
if (!mainNumber.matches("-?\\d*") || (fraction != null && !fraction.matches("\\d*"))) { | |
throw new IllegalArgumentException("illegal number: " + line); | |
} | |
int len = mainNumber.length(); | |
if (fraction != null) { | |
flen = fraction.length(); | |
} | |
if (len > MAXNUM || flen > MAXNUM) { | |
throw new IllegalArgumentException("number too large, max " + MAXNUM + " digits."); | |
} | |
int[] singular = new int[1]; | |
boolean rval = len > 0 ? unit(len, mainNumber, singular, result) : false; | |
if (fraction != null && flen > 0) { | |
boolean fractionNonZero = false; | |
for (int i = 0; i < flen; i++) { | |
if (fraction.charAt(i) != '0') { | |
fractionNonZero = true; | |
break; | |
} | |
} | |
if (fractionNonZero) { | |
if (rval) { | |
result.append((lflag ? " " : "and")).append(lflag ? " " : "\n"); | |
} | |
if (unit(flen, fraction, singular, result)) { | |
if (lflag) { | |
result.append(" "); | |
} | |
pfract(flen, singular[0], result); | |
rval = true; | |
} | |
} | |
} | |
if (!rval) { | |
result.append("zero").append(lflag ? "" : "."); | |
if (!lflag) result.append("\n"); | |
} | |
return result.toString(); | |
} | |
private boolean unit(int len, String p, int[] singular, StringBuilder result) { | |
boolean rval = false; | |
if (len > 3) { | |
if (len % 3 != 0) { | |
int off = len % 3; | |
len -= off; | |
if (number(p.substring(0, off), off, singular, result)) { | |
rval = true; | |
result.append(" ").append(name3[len / 3]).append(lflag ? " " : ".\n"); | |
} | |
p = p.substring(off); | |
} | |
while (len > 3) { | |
len -= 3; | |
if (number(p.substring(0, 3), 3, singular, result)) { | |
rval = true; | |
result.append(" ").append(name3[len / 3]).append(lflag ? " " : ".\n"); | |
} | |
p = p.substring(3); | |
} | |
} | |
if (number(p, len, singular, result)) { | |
if (rval) | |
singular[0] = 0; | |
if (!lflag) | |
result.append(".\n"); | |
rval = true; | |
} | |
return rval; | |
} | |
private boolean number(String p, int len, int[] singular, StringBuilder result) { | |
int val; | |
boolean rval = false; | |
singular[0] = 1; | |
switch (len) { | |
case 3: | |
if (p.charAt(0) != '0') { | |
rval = true; | |
singular[0] = 0; | |
result.append(name1[p.charAt(0) - '0']).append(" hundred"); | |
} | |
p = p.substring(1); | |
case 2: | |
val = (p.charAt(1) - '0') + (p.charAt(0) - '0') * 10; | |
if (val != 0) { | |
if (rval) | |
result.append(" "); | |
if (val < 20) | |
result.append(name1[val]); | |
else { | |
result.append(name2[val / 10]); | |
if (val % 10 != 0) | |
result.append("-").append(name1[val % 10]); | |
} | |
rval = true; | |
} | |
if (val != 1) | |
singular[0] = 0; | |
break; | |
case 1: | |
if (p.charAt(0) != '0') { | |
rval = true; | |
result.append(name1[p.charAt(0) - '0']); | |
} | |
if (p.charAt(0) != '1') | |
singular[0] = 0; | |
break; | |
} | |
return rval; | |
} | |
private void pfract(int len, int singular, StringBuilder result) { | |
String[] pref = {"", "ten-", "hundred-"}; | |
switch (len) { | |
case 1: | |
result.append("tenth"); | |
break; | |
case 2: | |
result.append("hundredth"); | |
break; | |
default: | |
result.append(pref[len % 3]).append(name3[len / 3]).append("th"); | |
break; | |
} | |
if (singular == 0) { | |
result.append("s"); | |
} | |
result.append(".\n"); | |
} | |
// Example usage (can be removed for library use) | |
public static void main(String[] args) { | |
NumberConverter converter = new NumberConverter(true); | |
System.out.println(converter.convert(1234567)); | |
System.out.println(converter.convert(123.456)); | |
System.out.println(converter.convert(-9876543210L)); | |
System.out.println(converter.convert("11111111111111111111111111111111111111111111111111111111111111111")); // Test MAXNUM | |
NumberConverter lConverter = new NumberConverter(true); | |
System.out.println(lConverter.convert(123)); | |
System.out.println(lConverter.convert(0.5)); //test fractions | |
System.out.println(lConverter.convert(new BigDecimal("123456789012345678901234567890"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment