在Java中将double转换为String [英] Convert double to String in Java

查看:1497
本文介绍了在Java中将double转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将双重值转换为字符串。

I am to convert a double value to a String.

double d=12345678.999;
String str = String.valueOf(d);

或者只是简单

String str = ""+d;

它是否以指数形式返回String值:

Does it, however in the exponent form-- returning the String value:

1.2345678999E7

我需要字符串中的值:

12345678.999

有没有办法呢?我可以解析字符串的指数,即'E'后的部分,然后从那里开始,但是必须有一个更简单的方法。

Is there a way to do this? I can parse the string for the value of exponent-- the portion after 'E' and go from there, but there must be an easier way of doing this.

/ / ===========================

//===========================

编辑:

输入中小数部分的长度不知道,可以是任意数字:

the length of the decimal part in the input isn't known, it can be any number of digits:

12345678.999

12345678.9999999

施加小数部分的格式将修复小数位数(?)

Imposing a format for the decimal part would fix the number of decimal digits(?)

推荐答案

您应该使用NumberFormat格式化字符串,使其不会以科学符号显示。

You should use NumberFormat to format Strings so that they do not show up in scientific notation.

String str = NumberFormat.getInstance().format(d);

这篇关于在Java中将double转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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