
我在EditText中显示了一个数字,该数字可以太长,最多可以包含20个字符(四进制).我希望数字的长度大于结尾处显示的数字,因此不会被截断.
例如:123456789将按原样显示
123456789123456789123456太长,将显示为1.123456789E8(仅作为示例!)
我已经测试过:
DecimalFormat df = new DecimalFormat("#.####");df.setMaximumIntegerDigits(20);但是20char之后,数字只是无法正确显示.例如:123456789123456789123456变成了56789123456789123456(以4个第一位数字分隔).
谢谢 !
解决方法:
Decimal Formater java doc描述如何处理指数.
ScIEntific Notation
Numbers in scIEntific notation are
expressed as the product of a mantissa
and a power of ten, for example, 1234
can be expressed as 1.234 x 10^3. The
mantissa is often in the range 1.0 <=
x < 10.0, but it need not be.
DecimalFormat can be instructed to
format and parse scIEntific notation
only via a pattern; there is currently
no factory method that creates a
scIEntific notation format. In a
pattern, the exponent character
immediately followed by one or more
digit characters indicates scIEntific
notation. Example: “0.###E0” formats
the number 1234 as “1.234E3”.
…
更为困难的部分是如何在普通计数法和科学计数法之间切换.
我通过在messageformater中的choIDe格式化程序中嵌入两个十进制格式化程序来完成此 *** 作:
messageformat format = new messageformat("{0,choice,0#{0,number,'#,##0.####'}|99999<{0,number,'000000.####E0'}}", Locale.ENGliSH);(此示例只有6个小数位,但是您可以更改它.)
消息格式的用法与十进制格式器有些不同,因为方法必须使用对象数组.
System.out.println(format.format(new Object[] { 123 }));它显示的(1,12,123,…)是:
11.1121231,23412,345123456E0123456.7E1123456.78E2123456.789E3123456.789E4123456.789E5123456.789E6您需要对模式进行一些调整,使其与您的20 diget要求相匹配,但是方法应该明确.
即使我证明它可以工作,我还是建议实现自己的格式化程序,该格式程序使用2个十进制格式化程序和一个if条件.
总结以上是内存溢出为你收集整理的java-如何确定DecimalFormat的最大大小,然后将其显示为指数?全部内容,希望文章能够帮你解决java-如何确定DecimalFormat的最大大小,然后将其显示为指数?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)