導航:首頁 > 編程語言 > java金額單位

java金額單位

發布時間:2021-03-05 14:12:56

java轉換數字以萬為單位

import java.util.Scanner;

/**

* 小於100000的不轉換,大於或等於100000的轉換為10萬,以此類推,110000轉為11萬,112000為11.2萬

* @author inferno

*

*/

public class Wan {


public static void main(String[] args) {

System.out.print("輸入一個專整數:");

Scanner scan = new Scanner(System.in);

long num = scan.nextLong();

if(num<100000){

System.out.println("您輸屬入的數字為:"+num);

}else{

double n = (double)num/10000;

System.out.println("您輸入的數字為:"+n+"萬");

}

}

}

㈡ java中如何查看每個部門金額以及合計,如何定義實體,如何定義集合

public static void main(String args[]) { //模擬10個不重復的商品加入集合 HashSet<Proct> data = new HashSet<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { int goodsId = random.nextInt(100); boolean isAdded = data.add(new Proct(goodsId, goodsId + "name", goodsId, "util", goodsId)); if (!isAdded) { i--; } } //如果知道一個id,得到所有信息? //將這個data轉化為map,鍵為id值為javabean HashMap<Integer, Proct> hashMap = new HashMap<>(); for (Proct item : data) { hashMap.put(item.goodsId, item); } //拿到id,get就行了 hashMap.get(??); }class Proct { /** * 商品編號 */ int goodsId; /** * 商品名稱 */ String name; /** * 商品價格 */ double price; /** * 商品單位 */ String util; /** * 商品數量 */ double num; public Proct(int goodsId, String name, double price, String util, double num) { this.goodsId = goodsId; this.name = name; this.price = price; this.util = util; this.num = num; } @Override public boolean equals(Object obj) { return this.goodsId == ((Proct) obj).goodsId; } @Override public String toString() { return "Proct{" + "goodsId=" + goodsId + ", name='" + name + '\'' + ", price=" + price + ", util='" + util + '\'' + ", num=" + num + '}'; }}

㈢ 用java財務軟體中最後的總金額是怎麼算的

public static void main(String args[]) {
.out.println("********************");
System.out.println("");
System.out.println("請選擇購買的商品編號:");
System.out.println("1.T恤(¥245.0) 網球鞋(¥570.0) 網球拍(¥500.0)");
System.out.println("********************");
System.out.println("");
double s = 0;
String d;
do {
System.out.print("請輸入商品編號:");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
if (a == 1) {
System.out.print("請輸入購買數量:");
int b = input.nextInt();
double c = 245 * b;
System.out.println("T恤¥245.0 數量:" + b + " " + "合計:" + c);
s = s + c;
} else if (a == 2) {
System.out.print("請輸入購買數量:");
int b = input.nextInt();
double c = 570 * b;
System.out.println("網球鞋¥245.0 數量:" + b + " " + "合計:" + c);
s = s + c;
} else if (a == 3) {
System.out.print("請輸入購買數量:");
int b = input.nextInt();
double c = 500 * b;
System.out.println("網球拍¥245.0 數量:" + b + " " + "合計:" + c);
s = s + c;
} else {
System.out.println("無此商品");
}
System.out.print("是否繼續(y/n)");
d = input.next();
System.out.println();
} while (d.equals("y"));
double m = s * 0.8;
System.out.println(":0.8");
System.out.println("應付金額:" + m);
System.out.print("實際金額:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
System.out.println();

double p = x - m;
System.out.println("找錢:" + p);
}
這樣顯示就和截圖一樣了

㈣ java 給輸入的數字加上單位

將double Depth;double Guess;改成String類型的,然後用next()接受控制台參數
取用的時候用substring(int begain,int end)分別截取數字部版分和單權位部分就行

㈤ java實現金額轉換,阿拉伯數字的金額轉換成中國傳統的形式

直接通過以下介面類方法實現即可:
import java.math.BigDecimal;
/**
* 金額工具
*
* @author zn
*
* @Date 2013-2-1
* @Email [email protected]
*/
public class MoneyUtil {

private static final int DFT_SCALE = 2;

/** 大寫數字 */
private static final String[] NUMBERS = { "零", "壹", "貳", "叄", "肆", "伍",
"陸", "柒", "捌", "玖" };
/** 整數部分的單位 */
private static final String[] IUNIT = { "元", "拾", "佰", "仟", "萬", "拾", "佰",
"仟", "億", "拾", "佰", "仟", "萬", "拾", "佰", "仟" };
/** 小數部分的單位 */
private static final String[] DUNIT = { "角", "分", "厘" };

/**
* 得到大寫金額。
*/
public static String toChinese(String str) {
str = str.replaceAll(",", "");// 去掉","
String integerStr;// 整數部分數字
String decimalStr;// 小數部分數字

// 初始化:分離整數部分和小數部分
if (str.indexOf(".") > 0) {
integerStr = str.substring(0, str.indexOf("."));
decimalStr = str.substring(str.indexOf(".") + 1);
} else if (str.indexOf(".") == 0) {
integerStr = "";
decimalStr = str.substring(1);
} else {
integerStr = str;
decimalStr = "";
}
// integerStr去掉首0,不必去掉decimalStr的尾0(超出部分捨去)
if (!integerStr.equals("")) {
integerStr = Long.toString(Long.parseLong(integerStr));
if (integerStr.equals("0")) {
integerStr = "";
}
}
// overflow超出處理能力,直接返回
if (integerStr.length() > IUNIT.length) {
System.out.println(str + ":超出處理能力");
return str;
}

int[] integers = toArray(integerStr);// 整數部分數字
boolean isMust5 = isMust5(integerStr);// 設置萬單位
int[] decimals = toArray(decimalStr);// 小數部分數字
return getChineseInteger(integers, isMust5)
+ getChineseDecimal(decimals);
}

/**
* 整數部分和小數部分轉換為數組,從高位至低位
*/
private static int[] toArray(String number) {
int[] array = new int[number.length()];
for (int i = 0; i < number.length(); i++) {
array[i] = Integer.parseInt(number.substring(i, i + 1));
}
return array;
}

/**
* 得到中文金額的整數部分。
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer("");
int length = integers.length;
for (int i = 0; i < length; i++) {
// 0出現在關鍵位置:1234(萬)5678(億)9012(萬)3456(元)
// 特殊情況:10(拾元、壹拾元、壹拾萬元、拾萬元)
String key = "";
if (integers[i] == 0) {
if ((length - i) == 13)// 萬(億)(必填)
key = IUNIT[4];
else if ((length - i) == 9)// 億(必填)
key = IUNIT[8];
else if ((length - i) == 5 && isMust5)// 萬(不必填)
key = IUNIT[4];
else if ((length - i) == 1)// 元(必填)
key = IUNIT[0];
// 0遇非0時補零,不包含最後一位
if ((length - i) > 1 && integers[i + 1] != 0)
key += NUMBERS[0];
}
chineseInteger.append(integers[i] == 0 ? key
: (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
}
return chineseInteger.toString();
}

/**
* 得到中文金額的小數部分。
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for (int i = 0; i < decimals.length; i++) {
// 捨去3位小數之後的
if (i == 3)
break;
chineseDecimal.append(decimals[i] == 0 ? ""
: (NUMBERS[decimals[i]] + DUNIT[i]));
}
return chineseDecimal.toString();
}

/**
* 判斷第5位數字的單位"萬"是否應加。
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length > 4) {
String subInteger = "";
if (length > 8) { // TODO 12-9-17
// 取得從低位數,第5到第8位的字串
subInteger = integerStr.substring(length - 8, length - 4);
} else {
subInteger = integerStr.substring(0, length - 4);
}
return Integer.parseInt(subInteger) > 0;
} else {
return false;
}
}

/**
* BigDecimal 相乘,四捨五入保留0位
*
* @param a
* @param b
* @return a*b
*/
public static BigDecimal mutiply(String a, String b, int roundingMode) {
BigDecimal bd = new BigDecimal(a);
return bd.multiply(new BigDecimal(b)).setScale(DFT_SCALE, roundingMode);
}

/**
* BigDecimal 相除,四捨五入保留兩位
*
* @param a
* @param b
* @return a/b
*/
public static BigDecimal div(String a, String b, int roundingMode) {
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
return decimal1.divide(decimal2, DFT_SCALE, roundingMode);
}

/**
* BigDecimal 相加,四捨五入保留兩位
*
* @param a
* @param b
* @return a+b
*/
public static BigDecimal sum(String a, String b, int roundingMode) {
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
// DecimalFormat format = new DecimalFormat("#0.00");
return decimal1.add(decimal2).setScale(DFT_SCALE, roundingMode);
}

/**
* BigDecimal 相減,四捨五入保留兩位
*
* @param a
* @param b
* @return a+b
*/
public static BigDecimal sub(String a, String b, int roundingMode) {
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
// DecimalFormat format = new DecimalFormat("#0.00");
return decimal1.subtract(decimal2).setScale(DFT_SCALE, roundingMode);
}

/**
* 100.00 為10000
*
* @param a
* @return
*/
public static BigDecimal format(String a, int roundingMode) {
return new BigDecimal(a).multiply(new BigDecimal(100)).setScale(0,
roundingMode);
}

public static void main(String[] args) {
String number = "54452";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "30200";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "30000.05";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "30000.00";
System.out.println(number + " " + MoneyUtil.toChinese(number));
}
}
備註:最後面的main方法是具體的調用。

㈥ java 中,如何編程實現千位分隔符,如1000000顯示成1,000,000

使用DecimalFormat類來實現,具體代碼如下:
import java.text.DecimalFormat;
class moneyFormat{
public static void main(String args[]){
long n =10000000;
DecimalFormat df = new DecimalFormat("#,###");
String m = df.format(n);
System.out.print(m);
}
}
輸出結果如下
10,000,000
一般金額是需要保留內兩位小容數,如果想保留小數把"#,###" 改成"#,###.00"

望採納!謝謝!

㈦ java中總金額怎麼實現

這個要看你具體的要求了,可以通過資料庫直接保存,也可以通過其他的金額在程序里通過java程序計算取得

㈧ 誰能提供個java金額轉換的程序

import java.math.BigDecimal;

public class MoneyChange {

/**
* Description 將數字金額轉換為中文金額
*
* @param
* <p>
* String Money 轉換前的數字金額
* </P>
* @return String MoneyStringToChineseCurrency("101.89")="壹佰零壹圓捌角玖分"
* MoneyStringToChineseCurrency("100.89")="壹佰零捌角玖分"
* MoneyStringToChineseCurrency("100")="壹佰圓整"
*/
public static String MoneyStringToChineseCurrency(String Money) {

// 中文金額單位數組
String[] straChineseUnit = new String[] { "分", "角", "圓", "拾", "佰", "仟",
"萬", "拾", "佰", "仟", "億", "拾", "佰", "仟" };
// 中文數字字元數組
String[] straChineseNumber = new String[] { "零", "壹", "貳", "叄", "肆",
"伍", "陸", "柒", "捌", "玖" };

BigDecimal bigdMoneyNumber = new BigDecimal(Money);

String strChineseCurrency = "";
// 零數位標記
boolean bZero = true;
// 中文金額單位下標
int ChineseUnitIndex = 0;

try {
if (bigdMoneyNumber.doubleValue() == 0)
return "零圓整";

// 處理小數部分,四捨五入
double doubMoneyNumber = Math
.round(bigdMoneyNumber.doubleValue() * 100);

// 是否負數
boolean bNegative = doubMoneyNumber < 0;

// 取絕對值
doubMoneyNumber = Math.abs(doubMoneyNumber);

// 循環處理轉換操作
while (doubMoneyNumber > 0) {
// 整的處理(無小數位)
if (ChineseUnitIndex == 2 && strChineseCurrency.length() == 0)
strChineseCurrency = strChineseCurrency + "整";

// 非零數位的處理
if (doubMoneyNumber % 10 > 0) {
strChineseCurrency = straChineseNumber[(int) doubMoneyNumber % 10]
+ straChineseUnit[ChineseUnitIndex]
+ strChineseCurrency;
bZero = false;
}
// 零數位的處理
else {
// 元的處理(個位)
if (ChineseUnitIndex == 2) {
// 段中有數字
if (doubMoneyNumber > 0) {
strChineseCurrency = straChineseUnit[ChineseUnitIndex]
+ strChineseCurrency;
bZero = true;
}
}
// 萬、億數位的處理
else if (ChineseUnitIndex == 6 || ChineseUnitIndex == 10) {
// 段中有數字
if (doubMoneyNumber % 1000 > 0)
strChineseCurrency = straChineseUnit[ChineseUnitIndex]
+ strChineseCurrency;
}

// 前一數位非零的處理
if (!bZero)
strChineseCurrency = straChineseNumber[0]
+ strChineseCurrency;

bZero = true;
}

doubMoneyNumber = Math.floor(doubMoneyNumber / 10);
ChineseUnitIndex++;
}

// 負數的處理
if (bNegative)
strChineseCurrency = "負" + strChineseCurrency;
} catch (Exception e) {
System.out.println("error!");
return "";
}

return strChineseCurrency;
}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(MoneyStringToChineseCurrency("15312.10"));
}

}
你試試看

㈨ java簡單題:一組金額數據,用人民幣大寫顯示出來

/**
*程序目的:
*從命令行接收一個數,並將其轉化為中文金額的大寫方式
*例如123.45-->壹佰貳拾叄元肆角伍分
*@authorgdb
*
*實在是不符合規范,程序裡面的演算法沒有讓人明白得很清楚的注釋,讀上去覺得有
*點難度,可讀性不強。而且很多程序還存在不少bug,隨便一測就測出來了。
*所以本人還是決定重新寫一下這個程序,並且盡量做到消除不必要的bug。這個程
*序我沒有用什麼很精妙的演算法,不過用了一些Java類庫中的類,像是正則表達式之類
*
*/
publicclassTrans2RMB{

/**
*測試程序的可行性
*@paramargs
*/
publicstaticvoidmain(String[]args){
System.out.println(" --------將數字轉換成中文金額的大寫形式------------ ");
Trans2RMBt2r=newTrans2RMB();
Strings=t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
//如果轉換過後是一個空串,則不輸出屏幕
if(!"".equals(s)){
System.out.println("轉換成中文後為:"+s);;
}
System.out.println(" ---------------------------------------------");
}

/**
*從命令行接收一個數,在其中調用checkNum()方法對其進行
*驗證,並返回相應的值
*@return如果輸入合法,返回輸入的這個數
*/
privateStringgetNum(){
Strings=null;
System.out.println("請輸入一個數字(精確到小數點後兩位):");
//從命令行輸入這個浮點數
java.util.Scannerscanner=newjava.util.Scanner(System.in);
s=scanner.next();
//關閉這個Scanner
scanner.close();
//判斷用戶輸入是否合法
//若合法,返回這個值;若非法返回"0"
if(this.checkNum(s)){
returns;
}else{
return"";
}
}

/**
*判斷用戶輸入的數據是否合法,用戶只能輸入大於零的數字,不能輸入其它字元
*@paramsString
*@return如果用戶輸入數據合法,返回true,否則返回false
*/
privatebooleancheckNum(Strings){
//如果用戶輸入的數里有非數字字元,則視為非法數據,返回false
try{
floatf=Float.valueOf(s);
//如果這個數小於零則視為非法數據,返回false
if(f<0){
System.out.println("非法數據,請檢查!");
returnfalse;
}else{
returntrue;
}
}catch(NumberFormatExceptione){
System.out.println("非法數據,請檢查!");
returnfalse;
}
}

/**
*把用戶輸入的數以小數點為界分割開來,並調用numFormat()方法
*進行相應的中文金額大寫形式的轉換
*註:傳入的這個數應該是經過roundString()方法進行了四捨五入操作的
*@paramsString
*@return轉換好的中文金額大寫形式的字元串
*/
privateStringsplitNum(Strings){
//如果傳入的是空串則繼續返回空串
if("".equals(s)){
return"";
}
//以小數點為界分割這個字元串
intindex=s.indexOf(".");
//截取並轉換這個數的整數部分
StringintOnly=s.substring(0,index);
Stringpart1=this.numFormat(1,intOnly);
//截取並轉換這個數的小數部分
StringsmallOnly=s.substring(index+1);
Stringpart2=this.numFormat(2,smallOnly);
//把轉換好了的整數部分和小數部分重新拼湊一個新的字元串
StringnewS=part1+part2;
returnnewS;
}

/**
*對傳入的數進行四捨五入操作
*@paramsString從命令行輸入的那個數
*@return四捨五入後的新值
*/
privateStringroundString(Strings){
//如果傳入的是空串則繼續返回空串
if("".equals(s)){
return"";
}
//將這個數轉換成double類型,並對其進行四捨五入操作
doubled=Double.parseDouble(s);
//此操作作用在小數點後兩位上
d=(d*100+0.5)/100;
//將d進行格式化
s=newjava.text.DecimalFormat("##0.000").format(d);
//以小數點為界分割這個字元串
intindex=s.indexOf(".");
//這個數的整數部分
StringintOnly=s.substring(0,index);
//規定數值的最大長度只能到萬億單位,否則返回"0"
if(intOnly.length()>13){
System.out.println("輸入數據過大!(整數部分最多13位!)");
return"";
}
//這個數的小數部分
StringsmallOnly=s.substring(index+1);
//如果小數部分大於兩位,只截取小數點後兩位
if(smallOnly.length()>2){
StringroundSmall=smallOnly.substring(0,2);
//把整數部分和新截取的小數部分重新拼湊這個字元串
s=intOnly+"."+roundSmall;
}
returns;
}

/**
*把傳入的數轉換為中文金額大寫形式
*@paramflagint標志位,1表示轉換整數部分,0表示轉換小數部分
*@paramsString要轉換的字元串
*@return轉換好的帶單位的中文金額大寫形式
*/
privateStringnumFormat(intflag,Strings){
intsLength=s.length();
//貨幣大寫形式
StringbigLetter[]={"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};
//貨幣單位
Stringunit[]={"元","拾","佰","仟","萬",
//拾萬位到仟萬位
"拾","佰","仟",
//億位到萬億位
"億","拾","佰","仟","萬"};
Stringsmall[]={"分","角"};
//用來存放轉換後的新字元串
StringnewS="";
//逐位替換為中文大寫形式
for(inti=0;i<sLength;i++){
if(flag==1){
//轉換整數部分為中文大寫形式(帶單位)
newS=newS+bigLetter[s.charAt(i)-48]+unit[sLength-i-1];
}elseif(flag==2){
//轉換小數部分(帶單位)
newS=newS+bigLetter[s.charAt(i)-48]+small[sLength-i-1];
}
}
returnnewS;
}

/**
*把已經轉換好的中文金額大寫形式加以改進,清理這個字
*符串裡面多餘的零,讓這個字元串變得更加可觀
*註:傳入的這個數應該是經過splitNum()方法進行處理,這個字
*符串應該已經是用中文金額大寫形式表示的
*@paramsString已經轉換好的字元串
*@return改進後的字元串
*/
privateStringcleanZero(Strings){
//如果傳入的是空串則繼續返回空串
if("".equals(s)){
return"";
}
//如果用戶開始輸入了很多0去掉字元串前面多餘的'零',使其看上去更符合習慣
while(s.charAt(0)=='零'){
//將字元串中的"零"和它對應的單位去掉
s=s.substring(2);
//如果用戶當初輸入的時候只輸入了0,則只返回一個"零"
if(s.length()==0){
return"零";
}
}
//字元串中存在多個'零'在一起的時候只讀出一個'零',並省略多餘的單位
/*由於本人對演算法的研究太菜了,只能用4個正則表達式去轉換了,各位大蝦別介意哈...*/
Stringregex1[]={"零仟","零佰","零拾"};
Stringregex2[]={"零億","零萬","零元"};
Stringregex3[]={"億","萬","元"};
Stringregex4[]={"零角","零分"};
//第一輪轉換把"零仟",零佰","零拾"等字元串替換成一個"零"
for(inti=0;i<3;i++){
s=s.replaceAll(regex1[i],"零");
}
//第二輪轉換考慮"零億","零萬","零元"等情況
//"億","萬","元"這些單位有些情況是不能省的,需要保留下來
for(inti=0;i<3;i++){
//當第一輪轉換過後有可能有很多個零疊在一起
//要把很多個重復的零變成一個零
s=s.replaceAll("零零零","零");
s=s.replaceAll("零零","零");
s=s.replaceAll(regex2[i],regex3[i]);
}
//第三輪轉換把"零角","零分"字元串省略
for(inti=0;i<2;i++){
s=s.replaceAll(regex4[i],"");
}
//當"萬"到"億"之間全部是"零"的時候,忽略"億萬"單位,只保留一個"億"
s=s.replaceAll("億萬","億");
returns;
}
}

閱讀全文

與java金額單位相關的資料

熱點內容
操作軟體映像文件如何下載 瀏覽:736
安卓logopsd 瀏覽:761
ipadmini2還原密碼 瀏覽:145
雲南精準扶貧大數據管理平台登陸 瀏覽:6
android支付微信介面開發 瀏覽:444
和女朋友一起看電影色色的電影 瀏覽:967
數控編程畢業可以干什麼 瀏覽:966
泰國永恆電影下載 瀏覽:306
大數據課程推薦 瀏覽:638
男主是吸血鬼的小說 瀏覽:192
玩網路游戲有什麼壞處 瀏覽:973
愛情電影院最新上映電影 瀏覽:199
大數據有關論文 瀏覽:80
他們要去電影院嗎英語 瀏覽:347
蘋果手機照片保險箱 瀏覽:509
免費的看片網站入囗 瀏覽:497
shs文件怎麼刪除 瀏覽:913
什麼優app黃色 瀏覽:292
大人變小孩的電影 瀏覽:852
想做老師有什麼app好用 瀏覽:485

友情鏈接