回合数只有第一位小数 [英] Round number to only first decimal place

查看:173
本文介绍了回合数只有第一位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道可用的手机内存,所以我写了这篇code。

I would like to know the available phone memory, so I wrote this code.

File path2 = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path.getPath());
long blockSize2 = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
double result = availableBlocks * blockSize;

free = (Preference)this.findPreference("free_mem");
free.setSummary(Double.toString(result)+" GB");

现在的问题是,我得到一个outpout为5.654707363。我试图把只用小数点后第一位。

The problem is that I get a outpout as 5.654707363. I tried to take only the first decimal place using

free = Math.round(size * 10) / 10d;

但不起作用。任何想法?

But doesn't work. Any ideas?

推荐答案

如果我理解你的问题的权利,你需要的NumberFormat 这里:

If I understood your question right you need NumberFormat here:

NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
nf.format(5.654707363);

生成的 5,7

这篇关于回合数只有第一位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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