
正文
java中的货币代码 java货币格式化
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java程序。 输入为CNY USD等货币缩写。 输出为 ¥ $等 该货币的符号。currency类
因为我们用的都是中文环境(默认),所以你的程序只能输入中国的货币符号,要通过Locale类的: public static void setDefault(Locale newLocale)方法设置下语言环境
具体代码可参考如下的:
import java.util.Currency;
import java.util.Locale;
import java.util.Scanner;
/**
*
* @author top
*/
public class CurrencySymbol {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Please input a valid ISO 4217 currency code: ");
Scanner scan = new Scanner(System.in);
String code1 = scan.nextLine();
Locale.setDefault(Locale.CHINA);//中文语言环境下
Currency currency1 = Currency.getInstance(code1);
System.out.println(currency1.getSymbol());
String code2 = scan.nextLine();
Locale.setDefault(Locale.US);//美国
Currency currency2 = Currency.getInstance(code2);
System.out.println(currency2.getSymbol());
}
}
相关问答
Q1: Java金额的中文大写方式
/**
* 金额小数转换成中文大写金额
* @author Neil Han
*
*/
public class ConvertMoneyToUppercase {
private static final String UNIT[] = { "万", "千", "佰", "拾", "亿", "千", "佰",
"拾", "万", "千", "佰", "拾", "元", "角", "分" };
private static final String NUM[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" };
private static final double MAX_VALUE = 9999999999999.99D;
/**
* 将金额小数转换成中文大写金额
* @param money
* @return result
*/
public static String convertMoney(double money) {
if (money 0 || money MAX_VALUE)
return "参数非法!";
long money1 = Math.round(money * 100); // 四舍五入到分
if (money1 == 0)
return "零元整";
String strMoney = String.valueOf(money1);
int numIndex = 0; // numIndex用于选择金额数值
int unitIndex = UNIT.length - strMoney.length(); // unitIndex用于选择金额单位
boolean isZero = false; // 用于判断当前为是否为零
String result = "";
for (; numIndex strMoney.length(); numIndex++, unitIndex++) {
char num = strMoney.charAt(numIndex);
if (num == '0') {
isZero = true;
if (UNIT[unitIndex] == "亿" || UNIT[unitIndex] == "万"
|| UNIT[unitIndex] == "元") { // 如果当前位是亿、万、元java中的货币代码,且数值为零
result = result + UNIT[unitIndex]; //补单位亿、万、元
isZero = false;
}
}else {
if (isZero) {
result = result + "零";
isZero = false;
}
result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
}
}
//不是角分结尾就加"整"字
if (!result.endsWith("角")!result.endsWith("分")) {
result = result + "整";
}
//例如没有这行代码,数值"400000001101.2",输出就是"肆千亿万壹千壹佰零壹元贰角"
result = result.replaceAll("亿万", "亿");
return result;
}
public static void main(String[] args) {
double value = Double.parseDouble("40330701101.2");
System.out.println("您输入java中的货币代码的金额(小写)为:" + value);
System.out.println("您输入的金额(大写)为:" + convertMoney(value));
}
}
Q2: 用java写一个汇率的程序:将人民币与美元、欧元、英镑等多种货币的汇率保存在指定文件中,设计图形用
程序:
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class test8{ public static void main(String[] args) { JFrame f=new JFrame(); f.setSize(200java中的货币代码, 200); JTextArea t1 = new JTextArea(1,5); JTextArea t2 = new JTextArea(1,5); JButton b1=new JButton("test it"); Label l1,l2; l1 = new Label(); l2 = new Label(); l1.setText("Please Enter The Number"); l2.setText("The Result Of The Test"); Choice c = new Choice(); c.add("RMB"); c.add("KRW"); c.add("DOLLAR"); Choice c1 = new Choice(); c1.add("RMB"); c1.add("KRW"); c1.add("DOLLAR"); f.add(l1); f.add(t1); f.add(c); f.add(l2); f.add(t2); f.add(c1); f.add(b1); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {if(c.getSelectedItem()=="RMB"c1.getSelectedItem()=="DOLLAR") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*0.1452; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="DOLLAR"c1.getSelectedItem()=="RMB") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*6.8866; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="RMB"c1.getSelectedItem()=="KRW") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*164.9824; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="KRW"c1.getSelectedItem()=="RMB") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*0.0061; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="DOLLAR"c1.getSelectedItem()=="KRW") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*1136.2500; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="KRW"c1.getSelectedItem()=="DOLLAR") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*0.0009; String r = Double.toString(w); t2.setText(r); } } } ); f.setLayout(new FlowLayout()); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
操作环境:java 版型号:8.64
拓展资料:
1、Java是一门面向对象编程语言,不仅吸收了C++语言java中的货币代码的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等
2、由于C++所具有的优势,该项目组的研究人员首先考虑采用C++来编写程序。但对于硬件资源极其匮乏的单片式系统来说,C++程序过于复杂和庞大。另外由于消费电子产品所采用的嵌入式处理器芯片的种类繁杂,如何让编写的程序跨平台运行也是个难题。为了解决困难,他们首先着眼于语言的开发,假设了一种结构简单、符合嵌入式应用需要的硬件平台体系结构并为其制定了相应的规范,其中就定义了这种硬件平台的二进制机器码指令系统(即后来成为“字节码”的指令系统),以待语言开发成功后,能有半导体芯片生产商开发和生产这种硬件平台。对于新语言的设计,Sun公司研发人员并没有开发一种全新的语言,而是根据嵌入式软件的要求,对C++进行了改造,去除了留在C++的一些不太实用及影响安全的成分,并结合嵌入式系统的实时性要求,开发了一种称为Oak的面向对象语言。
操作环境:java 版型号:8.64
操作环境:C++ 版型号:8.2.64
Q3: Java编程:最少钱币数,这是一个古老而又经典的问题,用给定的几种钱币
/**
* 简单用人民币的常见数额做了初始化
*/
public class Test {
public static void main(String[] args){
int[] commonMoney = {100,50,20,10,5,1};
System.out.println(getLeastNum(108,commonMoney));
}
static String getLeastNum(int targetMoney,int[] commonMoney){
StringBuffer buffer = new StringBuffer();
int num = 0,total = 0;
String result = targetMoney + "$ 最少需要 ";
for(int money : commonMoney){
if(targetMoney = money){
total += num = targetMoney/money;
targetMoney = targetMoney%money;
buffer.append("," + num + " 张 " + money + "$");
}
if(0 == targetMoney) break;
}
return result + total + " 张货币,分别是: " + buffer.substring(1);
}
}
输出:
108$ 最少需要 5 张货币,分别是: 1 张 100$,1 张 5$,3 张 1$
Q4: java怎么输出货币符号
java输出货比符号测试方法java中的货币代码:
import java.text.NumberFormat;
import java.util.Locale;
public class FormatTest {
public static void main(String args[]) {
// 不使用格式化输出数
double d = 10.0 / 3.0;
System.out.println("无格式化输出:" + d);
// 使用本地默认格式输出数
NumberFormat numberFormat = NumberFormat.getNumberInstance();
//numberFormat.setMaximumFractionDigits(4);
//numberFormat.setMinimumIntegerDigits(6);
String numberString = numberFormat.format(d);
System.out.println("本地默认格式输出数:" + numberString);
// 使用本地默认格式输出货币值
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
System.out.println("本地默认格式输出货币值:" + currencyFormat.format(d));
// 使用本地默认格式输出百分数
NumberFormat percentFormat = NumberFormat.getPercentInstance();
System.out.println("本地默认格式输出百分数:" + percentFormat.format(d));
// 在不同java中的货币代码的国家和地区数字表示的格式也有区别。如德国
// 使用德国的格式化输出数
NumberFormat numberFormatG = NumberFormat
.getNumberInstance(Locale.GERMANY);
System.out.println("德国数字输出形式:" + numberFormatG.format(d));
// 使用德国货币输出形式
NumberFormat currencyFormatG = NumberFormat
.getCurrencyInstance(Locale.GERMANY);
System.out.println("德国货币输出形式:" + currencyFormatG.format(d));
// 使用美国货币输出形式
NumberFormat currencyFormatA = NumberFormat
.getCurrencyInstance(Locale.US);
System.out.println("美国货币输出形式:" + currencyFormatA.format(d));
// 使用德国百分数输出形式
NumberFormat percentFormatG = NumberFormat
.getPercentInstance(Locale.GERMANY);
System.out.println("德国百分数输出形式:" + percentFormatG.format(d));
System.exit(0);
}
}
关于java中的货币代码和java货币格式化的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







