如何使用的DecimalFormat格式化钱? [英] How to use DecimalFormat to format money?

查看:431
本文介绍了如何使用的DecimalFormat格式化钱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入看起来像 8.7000000 ,我想格式化它看起来像 8.70欧元。 我认为使用的DecimalFormat 类:

the input looks like that 8.7000000 and I want to format it to look like that 8.70 EUR. I considered using the DecimalFormat class:

    Double number = Double.valueOf(text);

    DecimalFormat dec = new DecimalFormat("#.## EUR");
    String credits = dec.format(number);

    TextView tt = (TextView) findViewById(R.id.creditsView);
    tt.setText(credits);

结果是 8,7欧元。我怎么能告诉格式化后有两位数字的

The result is 8,7 EUR. How can I tell the formatter to have two digits after the ,?

推荐答案

也想在此强调乔恩斯基特的观点有关使用整型来存储所有的货币 - 不希望被处理金钱浮点错误

Also want to highlight here Jon Skeet's point about using integer to store all your currency - don't want to be dealing with floating point errors in money.

使用.00,而不是## - 0是指一个数字,#表示一个数字(但隐藏它,如果它等于零)

use .00 instead of .## - 0 means a digit, # means a digit (But hide it if it's equal to zero)

Double number = Double.valueOf(text);

DecimalFormat dec = new DecimalFormat("#.00 EUR");
String credits = dec.format(number);

TextView tt = (TextView) findViewById(R.id.creditsView);
tt.setText(credits);

使用setMinimumFractionDigits?

use setMinimumFractionDigits?

  Double number = Double.valueOf(text);

    DecimalFormat dec = new DecimalFormat("#.## EUR");
    dec.setMinimumFractionDigits(2);
    String credits = dec.format(number);

    TextView tt = (TextView) findViewById(R.id.creditsView);
    tt.setText(credits);

这篇关于如何使用的DecimalFormat格式化钱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆