自定义 Java 1.6 代码以在 Java 1.3 中工作
String.format("%,.2f", tranInfo.getAmount())
注意:tranInfo.getAmount() 返回 double 输入
上面的内容Java 代码在 Windows 平台的 Java 版本 1.6 中运行良好。
但是当我尝试将代码自定义为:
String.format("%,.2f",new Object[] {new Double(tranInfo.getAmount())})
以便与 < Sco OpenServer 5.0.6 平台中的 em>Java 版本 1.3。
使用 String.format() 方法在运行时总是出现错误
我的问题是:
任何 String.format() 代码在 java 1.3 中使用 (String,double) 吗?
但 Java 1.3 仅支持 String.format(String,Object[])
提前致谢。
String.format("%,.2f", tranInfo.getAmount())
Note : tranInfo.getAmount() return double type
the above Java code work fine in Java version 1.6 in Windows platform.
But when i try to customize the code to :
String.format("%,.2f",new Object[] {new Double(tranInfo.getAmount())})
in order to work with Java version 1.3 in Sco OpenServer 5.0.6 platform.
it always has an error on run-time with the method String.format()
my question is :
Any String.format() code to work with (String,double) in java 1.3 ?
but Java 1.3 supports only String.format(String,Object[])
Thank in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Java 6 中,您依靠可变参数和自动装箱将
double
转换为Object[]
。 Java 5 中引入了可变参数和自动装箱。假设,通过显式进行转换,您可以使用
double
参数使 String.format 在 Java 1.3 中工作。例如,这段代码可以在 Java 1.3 及更高版本中工作...除了 String.format 也只在 Java 1.5 中引入。
现在,如果您以某种方式让它工作,那么您使用的并不是真正的 Java 1.3.x。 (也许 SCO 的 Java 不是真正的 Java 1.3.x?也许您正在 Java 6 上编译,并将 -source 设置为 1.3?在后一种情况下,当您在 Java 1.3 上运行代码时可能会出现错误。
) ,不能保证为较新版本的 Java 编写的代码可以用较旧版本的 Java 进行编译。让新代码在旧平台上运行通常需要更改代码以避免使用较新的语言功能和 API。
SCO 实际上是一个死平台……被 SCO 试图从 Linux 世界勒索金钱的愚蠢行为所杀死。强烈建议您将应用程序迁移到其他地方。
In Java 6 you are relying on varargs and auto-boxing to turn
double
intoObject[]
. Varargs and autoboxing were introduced in Java 5.Hypothetically, you could get String.format to work in Java 1.3 with a
double
argument, by doing the conversion explicitly; e.g.This code would work in Java 1.3 and later versions ... except that String.format was only introduced in Java 1.5 too.
Now if you are somehow getting this to work, then you are not using something that is truly Java 1.3.x. (Perhaps, SCO's Java is not true Java 1.3.x? Perhaps you are compiling on Java 6 with -source set to 1.3? In the latter case, you are likely to get errors when you run the code on Java 1.3.)
In general, there is no guarantee that code written for a newer version of Java will compile with an older version of Java. Getting new code to run on an old platform will typically involve changing the code to avoid the use of newer language features and APIs.
SCO is effectively a dead platform ... killed by SCO's stupidity in trying to extort money from the Linux world. You would be well advised to migrate your applications to something else.