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.

  1. 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 , #.##.

  2. 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 argument system.double, not java.lang.double, in order formatting work (see http://www.functionx.com/jsharp/lesson04.htm).


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -