Formatting double to 2 digits after decimal point in J# -
how format double
value in j#
2 digits after decimal point (without doing arithmetic operations)?
double x = 3.333333; string s = string.format("\rwork done: {0}%", new double(x)); system.out.print(s);
i thought j#
identical java
, following java
code gives different result j#
:
double x = 3.333333; string s = string.format("\rwork done %1$.2f%%", x); system.out.print(s);
(since j#
dead , unsupported, use visual j# 2005
)
string.format()
api introduced in java 1.5, there's no chance can use in visual j++ or visual j#.
there're 2 approaches problem.
using java 1.1 api (works java, j++ , j#):
import java.text.messageformat; /* ... */ final double d = 3.333333d; system.out.println(messageformat.format("{0,number,#.##}", new object[] {new double(d)})); system.out.println(messageformat.format("{0,number,0.00}", new object[] {new double(d)}));
note despite both formats work given double, there's difference between
0.00
,#.##
.using .net api. here's c# code fragment need:
using system; /* ... */ const double d = 3.333333d; console.writeline(string.format("{0:f2}", d)); console.writeline(string.format("{0:0.00}", d)); console.writeline(string.format("{0:0.##}", d));
now, same code translated j#:
import system.console; /* ... */ final double d = 3.333333d; console.writeline(string.format("work done {0:f2}%", (system.double) d)); console.writeline(string.format("{0:work done 0.00}%", (system.double) d)); console.writeline(string.format("{0:work done #.##}%", (system.double) d));
note need cast
double
argumentsystem.double
, notjava.lang.double
, in order formatting work (see http://www.functionx.com/jsharp/lesson04.htm).
Comments
Post a Comment