【问题标题】:How do I format 2 long values into percentage formats?如何将 2 个长值格式化为百分比格式?
【发布时间】:2013-06-08 18:29:00
【问题描述】:

考虑这种情况。

我有 2 个长变量 a 和 b。
我正在尝试以这些格式获取百分比:

xx.x 和 0.xx。

我尝试将两者都转换为双倍和除法,但我没有采用后一种格式。

【问题讨论】:

标签: java long-integer percentage


【解决方案1】:

类似的东西?

long a = 12, d = 34;
double ratio = a / (double) d;
DecimalFormat ratioFormat = new DecimalFormat("#.##");
DecimalFormat percentFormat= new DecimalFormat("#.#%");
System.out.println("ratio = " + ratioFormat.format(ratio));
System.out.println("percent = " + percentFormat.format(ratio));

哪些输出:

ratio = 0.35
percent = 35.3%

【讨论】:

猜你喜欢
  • 2019-07-03
  • 2015-09-30
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多