Java實(shí)現(xiàn)數(shù)字金額轉(zhuǎn)化為英文金額功能
1. 需求分析
假設(shè)我們需要一個(gè)方法,輸入一個(gè)double類(lèi)型的數(shù)值(代表金額),輸出其對(duì)應(yīng)的英文表述。例如,輸入??123456.78??,輸出應(yīng)為??One Hundred Twenty Three Thousand Four Hundred Fifty Six Dollars and Seventy Eight Cents??。
2. 技術(shù)選型
- 編程語(yǔ)言:Java
- 開(kāi)發(fā)工具:IntelliJ IDEA
- 測(cè)試工具:JUnit
3. 實(shí)現(xiàn)思路
3.1 分解數(shù)字
首先,將數(shù)字分解成整數(shù)部分和小數(shù)部分。對(duì)于整數(shù)部分,按照千位分組,每組分別轉(zhuǎn)換成英文;對(duì)于小數(shù)部分,直接轉(zhuǎn)換成英文。
3.2 數(shù)字到英文的映射
創(chuàng)建兩個(gè)數(shù)組,分別存儲(chǔ)0-19和20-90的英文表示,以及一個(gè)字符串?dāng)?shù)組存儲(chǔ)千、百萬(wàn)等單位的英文表示。
3.3 轉(zhuǎn)換邏輯
- 對(duì)于小于20的數(shù),直接從數(shù)組中獲取英文表示。
- 對(duì)于20到99之間的數(shù),先處理十位,再處理個(gè)位。
- 對(duì)于100以上的數(shù),遞歸地處理百位及以上的部分。
4. 代碼實(shí)現(xiàn)
public class NumberToWordsConverter { private static final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private static final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private static final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) { words = helper(num % 1000) + THOUSANDS[i] + " " + words; } num /= 1000; i++; } return words.trim(); } private String helper(int num) { if (num == 0) return ""; else if (num < 20) return LESS_THAN_20[num] + " "; else if (num < 100) return TENS[num / 10] + " " + helper(num % 10); else return LESS_THAN_20[num / 100] + " Hundred " + helper(num % 100); } public String convert(double amount) { long integerPart = (long) amount; double decimalPart = amount - integerPart; String integerWords = numberToWords((int) integerPart); String decimalWords = numberToWords((int) (decimalPart * 100)); return integerWords + " Dollars and " + decimalWords + " Cents"; } }
5. 測(cè)試
為了確保我們的轉(zhuǎn)換功能正確無(wú)誤,可以編寫(xiě)一些單元測(cè)試來(lái)驗(yàn)證:
import org.junit.Test; import static org.junit.Assert.assertEquals; public class NumberToWordsConverterTest { @Test public void testConvert() { NumberToWordsConverter converter = new NumberToWordsConverter(); assertEquals("One Hundred Twenty Three Thousand Four Hundred Fifty Six Dollars and Seventy Eight Cents", converter.convert(123456.78)); assertEquals("One Dollar and Twenty Three Cents", converter.convert(1.23)); assertEquals("Zero Dollars and Zero Cents", converter.convert(0.00)); } }
我們成功實(shí)現(xiàn)了將數(shù)字金額轉(zhuǎn)換為英文描述的功能。這個(gè)功能在實(shí)際應(yīng)用中非常有用,特別是在處理國(guó)際化的財(cái)務(wù)系統(tǒng)時(shí)。應(yīng)用中,將數(shù)字金額轉(zhuǎn)換為英文金額的需求常見(jiàn)于銀行系統(tǒng)、財(cái)務(wù)軟件等場(chǎng)景。這種功能通常用于生成正式的財(cái)務(wù)文件,如支票、發(fā)票等,以確保金額的準(zhǔn)確性和防篡改性。
下面是一個(gè)簡(jiǎn)單的Java示例代碼,展示如何將數(shù)字金額(例如123456.78)轉(zhuǎn)換為英文金額(例如"One Hundred Twenty Three Thousand Four Hundred Fifty Six Dollars and Seventy Eight Cents")。這個(gè)示例使用了基本的字符串處理和數(shù)組映射來(lái)實(shí)現(xiàn)轉(zhuǎn)換邏輯。
import java.util.HashMap; import java.util.Map; public class NumberToWordsConverter { private static final String[] unitsMap = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; private static final String[] tensMap = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; private static final Map<Long, String> thousandsMap = new HashMap<>(); static { thousandsMap.put(1000000000L, "Billion"); thousandsMap.put(1000000L, "Million"); thousandsMap.put(1000L, "Thousand"); thousandsMap.put(1L, ""); } public static void main(String[] args) { double amount = 123456.78; System.out.println("Amount in words: " + convertNumberToWords(amount)); } public static String convertNumberToWords(double number) { long integerPart = (long) number; int decimalPart = (int) Math.round((number - integerPart) * 100); StringBuilder result = new StringBuilder(); if (integerPart == 0) { result.append("Zero Dollars"); } else { result.append(convertLessThanOneThousand(integerPart)).append(" Dollars"); } if (decimalPart > 0) { result.append(" and ").append(convertLessThanOneThousand(decimalPart)).append(" Cents"); } return result.toString().trim(); } private static String convertLessThanOneThousand(long number) { if (number % 100 < 20) { return unitsMap[(int) number % 100]; } else { return tensMap[(int) (number % 100 / 10)] + " " + unitsMap[(int) (number % 10)]; } } private static String convert(long number) { if (number == 0) { return ""; } for (Map.Entry<Long, String> entry : thousandsMap.entrySet()) { if (number >= entry.getKey()) { return convert(number / entry.getKey()) + " " + entry.getValue() + " " + convert(number % entry.getKey()); } } return ""; } }
代碼解釋?zhuān)?/strong>
- unitsMap 和 tensMap:這兩個(gè)數(shù)組分別存儲(chǔ)了個(gè)位數(shù)和十位數(shù)的英文表示。
- thousandsMap:這是一個(gè)哈希表,用于存儲(chǔ)千位以上的單位(如Thousand, Million, Billion)。
- convertNumberToWords:這是主函數(shù),負(fù)責(zé)將數(shù)字金額轉(zhuǎn)換為英文表示。它首先處理整數(shù)部分,然后處理小數(shù)部分。
- convertLessThanOneThousand:這個(gè)輔助函數(shù)用于將小于1000的數(shù)字轉(zhuǎn)換為英文。
- convert:這個(gè)遞歸函數(shù)用于處理大于1000的數(shù)字,并根據(jù)千位單位進(jìn)行分割。
通過(guò)上述代碼,你可以將任何有效的數(shù)字金額轉(zhuǎn)換為其對(duì)應(yīng)的英文表示形式。將Java中的數(shù)字金額轉(zhuǎn)換為英文金額是一個(gè)常見(jiàn)的需求,尤其是在處理財(cái)務(wù)報(bào)告、發(fā)票等需要精確表示金額的場(chǎng)景中。下面我將詳細(xì)介紹如何在Java中實(shí)現(xiàn)這一功能。
實(shí)現(xiàn)思路
- 拆分整數(shù)部分和小數(shù)部分:首先,將輸入的金額字符串按照小數(shù)點(diǎn)拆分為整數(shù)部分和小數(shù)部分。
- 定義數(shù)字和單位的映射:創(chuàng)建一個(gè)映射,將數(shù)字0-19以及100、1000、1000000等特殊單位映射到英文單詞。
- 處理整數(shù)部分:遞歸地將整數(shù)部分轉(zhuǎn)換為英文單詞。
- 處理小數(shù)部分:直接將小數(shù)部分的兩個(gè)數(shù)字轉(zhuǎn)換為英文單詞。
- 組合結(jié)果:將整數(shù)部分和小數(shù)部分的英文單詞組合起來(lái),形成最終的英文金額表示。
代碼實(shí)現(xiàn)
import java.util.HashMap; import java.util.Map; public class NumberToWordsConverter { private static final String[] LESS_THAN_20 = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; private static final String[] TENS = { "", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; private static final String[] THOUSANDS = { "", "Thousand", "Million", "Billion" }; public static String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) { words = helper(num % 1000) + THOUSANDS[i] + " " + words; } num /= 1000; i++; } return words.trim(); } private static String helper(int num) { if (num == 0) { return ""; } else if (num < 20) { return LESS_THAN_20[num] + " "; } else if (num < 100) { return TENS[num / 10] + " " + helper(num % 10); } else { return LESS_THAN_20[num / 100] + " Hundred " + helper(num % 100); } } public static String convertAmountToWords(double amount) { long integerPart = (long) amount; int decimalPart = (int) ((amount - integerPart) * 100); String integerWords = numberToWords((int) integerPart); String decimalWords = numberToWords(decimalPart); return integerWords.trim() + " Dollars and " + decimalWords.trim() + " Cents"; } public static void main(String[] args) { double amount = 1234.56; System.out.println(convertAmountToWords(amount)); } }
代碼解釋
- 常量定義:
- ?
?LESS_THAN_20?
?:包含0到19的英文單詞。 - ?
?TENS?
?:包含10到90的英文單詞。 - ?
?THOUSANDS?
?:包含千、百萬(wàn)、十億等單位的英文單詞。
numberToWords
?方法:
- 將整數(shù)部分轉(zhuǎn)換為英文單詞。通過(guò)遞歸處理每個(gè)三位數(shù)的部分,并加上相應(yīng)的單位(千、百萬(wàn)、十億)。
helper
?方法:
- 處理小于1000的數(shù)字,將其轉(zhuǎn)換為英文單詞。
convertAmountToWords
?方法:
- 將輸入的金額拆分為整數(shù)部分和小數(shù)部分。
- 調(diào)用?
?numberToWords?
?方法分別將整數(shù)部分和小數(shù)部分轉(zhuǎn)換為英文單詞。 - 組合結(jié)果,形成最終的英文金額表示。
main
?方法:
- 測(cè)試示例,將1234.56轉(zhuǎn)換為英文金額表示。
輸出結(jié)果
運(yùn)行上述代碼,輸出結(jié)果為:
One Thousand Two Hundred Thirty Four Dollars and Fifty Six Cents
這個(gè)實(shí)現(xiàn)可以處理大多數(shù)常見(jiàn)的金額轉(zhuǎn)換需求。如果需要處理更大的金額或更復(fù)雜的單位,可以進(jìn)一步擴(kuò)展和優(yōu)化。
到此這篇關(guān)于Java實(shí)現(xiàn)數(shù)字金額轉(zhuǎn)化為英文金額功能的文章就介紹到這了,更多相關(guān)Java數(shù)字金額轉(zhuǎn)英文金額內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java+socket實(shí)現(xiàn)簡(jiǎn)易局域網(wǎng)聊天室
這篇文章主要為大家詳細(xì)介紹了java+socket實(shí)現(xiàn)簡(jiǎn)易局域網(wǎng)聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05SpringMvc3+extjs4實(shí)現(xiàn)上傳與下載功能
這篇文章主要為大家詳細(xì)介紹了SpringMvc3+extjs4實(shí)現(xiàn)上傳與下載功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06java設(shè)計(jì)模式之簡(jiǎn)單工廠模式詳解
這篇文章主要介紹了java設(shè)計(jì)模式之簡(jiǎn)單工廠模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09java設(shè)計(jì)模式之適配器模式(Adapter)
這篇文章主要介紹了java設(shè)計(jì)模式之適配器模式Adapter的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01idea開(kāi)啟mybatis控制臺(tái)SQL日志打印的代碼示例
本文主要介紹了idea開(kāi)啟mybatis控制臺(tái)SQL日志打印的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12Java編程思想里的泛型實(shí)現(xiàn)一個(gè)堆棧類(lèi) 分享
這篇文章介紹了Java編程思想里的泛型實(shí)現(xiàn)一個(gè)堆棧類(lèi),有需要的朋友可以參考一下2013-07-07