将双精度型转换为不带小数位的字符串的最佳方法 [英] Best way to convert a double to String without decimal places

查看:61
本文介绍了将双精度型转换为不带小数位的字符串的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将双精度型转换为不带小数位的字符串的最佳方法是什么?

What is the best way to convert a double to String without decimal places?

String.valueOf((int)documentNumber)怎么办?

双精度数始终在小数点后为0.我不需要四舍五入

The doubles always have 0 after the decimal dot. I don't need to round or truncate

推荐答案

如果您确定double确实是整数,请使用以下整数:

If you are sure that the double is indeed an integer use this one:

NumberFormat nf = DecimalFormat.getInstance();
nf.setMaximumFractionDigits(0);
String str = nf.format(documentNumber);

作为奖励,这样一来,您可以将语言环境的配置保留为千位分隔符.

As a bonus, this way you keep your locale's configuration as in thousand separator.

编辑
我添加了此以前删除的选项,因为它似乎对OP很有用:

EDIT
I add this previously removed option as it seems that was useful to the OP:

Double.valueOf(documentNumber).intValue();

这篇关于将双精度型转换为不带小数位的字符串的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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