java 可以在没有科学记数法的情况下格式化 Double 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5018246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:04:28  来源:igfitidea点击:

Can a Double be formatted without scientific notation?

javastringdoublehibernate-search

提问by camiloqp

I am aware of the various posts floating out there with regards to the same issue.

我知道关于同一问题的各种帖子。

Mine its a little bit different and it might be a little obvious, but I will need your comments.

我的有点不同,可能有点明显,但我需要你的评论。

I am currently using Hibernate Searchand Luceneto Index entity properties.

我目前正在使用Hibernate SearchLucene来索引实体属性。

I have a bunch of Doubleproperties on my entities.

我的实体上有一堆Double属性。

These entities using the default Bridges from Lucene(Bridge i.e the one in charge converting LongToStringand StringToLong) are giving me troubles once the scientific notation starts to be used.

一旦开始使用科学记数法,这些使用来自Lucene的默认桥接器(桥接器,即负责转换LongToStringStringToLong 的实体)给我带来了麻烦。

I am trying to show on DataTableson a .xhtmlCredit and Debit amounts, their lenght can be as long as 18 digits, and their DataBase (DB2) type is BIGINT.

我想显示在数据表上。xhtmlCredit 和 Debit 金额,其长度可达 18 位,DataBase ( DB2) 类型为BIGINT

  1. I can not change the DataBase type to Long for example.
  2. I can not change either the Double type attributes of my entities either to for example Long
  1. 例如,我无法将数据库类型更改为 Long。
  2. 我无法将实体的 Double 类型属性更改为例如 Long

So whats the question?Is there a way from a String say "1234567890" to retrieve a Double whose format is 1234567890 and not 1.23456789E9 as it is being done by default by Double.parseDouble(FormattedString)?

那么问题是什么?有没有办法从字符串说“1234567890”来检索格式为 1234567890 而不是 1.23456789E9 的 Double ,因为它是默认完成的Double.parseDouble(FormattedString)

PD: I am aware of the existance of DecimalFormat, however take into account using this formater will give me a String formated correctly say : "#######.E0" but what I really need is a Double with such format, however when doing Double.parseDouble(FormattedString)I will loose such format.

PD:我知道DecimalFormat的存在,但是考虑到使用这个格式化程序会给我一个正确格式化的字符串说:“#######.E0”但我真正需要的是一个具有这种格式的 Double,但是,当Double.parseDouble(FormattedString)我这样做时,我会丢失这种格式。

Hope I was clear and thanks for any help.

希望我很清楚,并感谢您的帮助。

回答by aioobe

Is there a way from a String say "1234567890" to retrieve a Double whose value is 1234567890 and not 1.23456789E9 as it is being done by default by Double.parseDouble(FormattedString)?

有没有办法从 String 说“1234567890”来检索一个 Double ,它的值是 1234567890 而不是 1.23456789E9 因为它是默认完成的Double.parseDouble(FormattedString)

Your question doesn't really make sense. 1234567890 is the same value as 1.23456789E9 and a double represents one of them, if and only if it also represents the other.

你的问题真的没有意义。1234567890 与 1.23456789E9 的值相同,双精度表示其中之一,当且仅当它也表示另一个。

I am aware of the existance of DecimalFormat, however take into account using this formater will give me a String formated correctly say : "#######.E0" but what I really need is a Double with such format, however when doing Double.parseDouble(FormatedString) I will loose such format.

我知道 DecimalFormat 的存在,但是考虑到使用这个格式化程序会给我一个正确格式化的字符串说:“#######.E0”但我真正需要的是一个具有这种格式的 Double,但是当做 Double.parseDouble(FormatedString) 我会失去这样的格式。

No, there is no way to construct a Doubleso that it is displayed in a certain way. The toStringmethod for Doubleis what it is, and it can't be changed.

不,没有办法构造 aDouble使其以某种方式显示。该toString对方法Double是它是什么,它不能改变。

The only thing you can do is to for instance use DecimalFormator String.formatbut as you've noted, you'll always end up with a String.

您唯一可以做的就是例如使用DecimalFormatorString.format但正如您所指出的,您将始终以String.

回答by Jens Schauder

Don't know nothing of Lucene, but you can never have a Double in a .xhtml Document it is always a characterstring. A Double doesn't have a Format, only a String representation of a Double has.

对 Lucene 一无所知,但是您永远不能在 .xhtml 文档中使用 Double 它始终是一个字符串。Double 没有格式,只有 Double 的字符串表示形式有。

回答by camiloqp

So I finally got the solution to my problem. After rounding up what aioobeand Jens Schaudersaid. I am able to format the text dynamically on my .xhtml with the following tag:

所以我终于得到了解决我的问题的方法。在整理了aioobeJens Schauder所说的内容之后。我可以使用以下标签在我的 .xhtml 上动态格式化文本:

<h:outputText value="#{recordTable[column.property]}"
              rendered="#{column.header ne 'Details' and
                          column.header eq ('Total Credit Amount' or
                                            'Total Debit Amount')}">
              <f:convertNumber pattern="########"/>
</h:outputText>

Thanks for making clear to me these basic stuff I had blurred :)

感谢您向我澄清这些我已经模糊的基本内容:)