首页 > Java > java教程 > Java 的标准数学库是否提供将数字转换为其文字表示形式的函数?

Java 的标准数学库是否提供将数字转换为其文字表示形式的函数?

Mary-Kate Olsen
发布: 2024-12-30 11:14:19
原创
397 人浏览过

Does Java's standard math library offer a function to convert numbers to their word representations?

如何在 java 中将数字转换为单词

背景

目前,已经有一个基本的机制可以使用静态数组将数字转换为单词,根据数字的大小将它们翻译成英文文本。然而,这种方法遇到了异常大的数字的挑战。

<br>10183 = 一万一百八十三<br>90 = 九十<br>5888 = 五千八百八十八<br>

问题

数学库中是否有一个简单的函数可以用于此目的?

答案

这是代码,我认为SE中没有任何方法。

它本质上是将数字转换为字符串,解析它,并将其与其权重关联起来。

例如

<br>1000<br>&lt ;/pre><p>1被视为千位,1映射到由于其位置,“一”和千。</p><p>这是来自网站的代码:</p><p><strong>英语</strong></p><p></p><pre class="brush:php;toolbar:false"><br> import java.text.DecimalFormat;<p>公共类 EnglishNumberToWords {</p><p>私有静态最终字符串[]tensNames = {</p><pre class="brush:php;toolbar:false">"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
登录后复制

};

私有静态最终字符串[]numNames = {

"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
登录后复制

};

私人 EnglishNumberToWords() {}

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;
登录后复制

}

public static String Convert(long number) {

// 0 to 999 999 999 999
if (number == 0) { return "zero"; }

String snumber = Long.toString(number);

// pad with "0"
String mask = "000000000000";
DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number);

// XXXnnnnnnnnn
int billions = Integer.parseInt(snumber.substring(0,3));
// nnnXXXnnnnnn
int millions  = Integer.parseInt(snumber.substring(3,6));
// nnnnnnXXXnnn
int hundredThousands = Integer.parseInt(snumber.substring(6,9));
// nnnnnnnnnXXX
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!
return result.replaceAll("^\s+", "").replaceAll("\b\s{2,}\b", " ");
登录后复制

}

/**

  • 测试
  • @param args
    */

public static void main(String[] args) {

System.out.println("*** " + EnglishNumberToWords.convert(0));
System.out.println("*** " + EnglishNumberToWords.convert(1));
System.out.println("*** " + EnglishNumberToWords.convert(16));
System.out.println("*** " + EnglishNumberToWords.convert(100));
System.out.println("*** " + EnglishNumberToWords.convert(118));
System.out.println("*** " + EnglishNumberToWords.convert(200));
System.out.println("*** " + EnglishNumberToWords.convert(219));
System.out.println("*** " + EnglishNumberToWords.convert(800));
System.out.println("*** " + EnglishNumberToWords.convert(801));
System.out.println("*** " + EnglishNumberToWords.convert(1316));
System.out.println("*** " + EnglishNumberToWords.convert(1000000));
System.out.println("*** " + EnglishNumberToWords.convert(2000000));
System.out.println("*** " + EnglishNumberToWords.convert(3000200));
System.out.println("*** " + EnglishNumberToWords.convert(700000));
System.out.println("*** " + EnglishNumberToWords.convert(9000000));
System.out.println("*** " + EnglishNumberToWords.convert(9001000));
System.out.println("*** " + EnglishNumberToWords.convert(123456789));
System.out.println("*** " + EnglishNumberToWords.convert(2147483647));
System.out.println("*** " + EnglishNumberToWords.convert(3000000010L));

/*
 *** zero
 *** one
 *** sixteen
 *** one hundred
 *** one hundred eighteen
 *** two hundred
 *** two hundred nineteen
 *** eight hundred
 *** eight hundred one
 *** one thousand three hundred sixteen
 *** one million
 *** two millions
 *** three millions two hundred
 *** seven hundred thousand
 *** nine millions
 *** nine millions one thousand
 *** one hundred twenty three millions four hundred
 **      fifty six thousand seven hundred eighty nine
 *** two billion one hundred forty seven millions
 **      four hundred eighty three thousand six hundred forty seven
 *** three billion ten
 **/
登录后复制

}
}

Français
与英文版有很大不同,但法语版很多更多的困难!

<br>package com.rgagnon.howto;</p>
<p>导入 java.text.*;</p>
<p>class FrenchNumberToWords {<br> private static final字符串[] dizaineNames = {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">"",
"",
"vingt",
"trente",
"quarante",
"cinquante",
"soixante",
"soixante",
"quatre-vingt",
"quatre-vingt"
登录后复制

};

私有静态最终字符串[]uniteNames1 = {

"",
"un",
"deux",
"trois",
"quatre",
"cinq",
"six",
"sept",
"huit",
"neuf",
"dix",
"onze",
"douze",
"treize",
"quatorze",
"quinze",
"seize",
"dix-sept",
"dix-huit",
"dix-neuf"
登录后复制

};

私有静态最终字符串[] unityNames2 = {

"",
"",
"deux",
"trois",
"quatre",
登录后复制

以上是Java 的标准数学库是否提供将数字转换为其文字表示形式的函数?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板