java里面怎么format number?double i,j;
要把i/j的结果format成含有2位小数的格式, 例如 3.03, 4.00, 120.32 之类的格式.
请问怎么做?
Thanks~[辰星 (1-21 0:02, Long long ago)]
[ 传统版 |
sForum ][登录后回复]1楼
hereString pattern = "###.##";
double result = i / j;
DecimalFormat Formatter = new DecimalFormat(pattern);
String output = Formatter.format(result);
System.out.println(output);
[SmellsLikeTeenSpirit (1-21 0:20, Long long ago)]
[ 传统版 |
sForum ][登录后回复]2楼
如果你是说CS1102 lab 0的话import java.io.*;
import java.util.*;
import java.text.NumberFormat;
class Q4 {
//main class
public static void main(String []args) throws Exception{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String thisLine;
StringTokenizer st;
double a, b;
boolean blank = false;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
while((thisLine=stdin.readLine())!=null){
st = new StringTokenizer(thisLine);
a = Double.parseDouble(st.nextToken());
b = Double.parseDouble(st.nextToken());
if (blank)
System.out.print(" ");
else
blank = true;
System.out.print(nf.format(a/b));
}
System.out.println();
}
}[Climbing (1-21 0:46, Long long ago)]
[ 传统版 |
sForum ][登录后回复]3楼
Quite lots of ways..^^^Try to get familiar with some funny stuffs, like:
class test {
public static void main (String args[]) {
double i = 0.5;
double j = 0.3;
Double result = new Double (i/j);
String output = result.toString();
int index = output.indexOf('.');
System.out.println (output.substring(0,index+1+2));
}//main
}[maserati (1-21 1:15, Long long ago)]
[ 传统版 |
sForum ][登录后回复]4楼
(引用 maserati:Quite lots of ways..^^^Try to get familiar with some funny stuffs, like:
class test {
public static void main (String args[]...)[SUGGESTION]THIS IS WRONG!3.345 -> 3.34 [CORRECT]
3.356 -> 3.35 [WRONG, SHOULD BE 3.36][不告诉你 (1-21 11:05, Long long ago)]
[ 传统版 |
sForum ][登录后回复]5楼
(引用 Climbing:如果你是说CS1102 lab 0的话import java.io.*;
import java.util.*;
import java.text.NumberFormat;
class Q4 {
//main class
public...)hey!!! 你怎么干这种事?你这不是还别人吗?ZWB会查的,逮到就死了!!!
害人精![采心 (1-21 13:15, Long long ago)]
[ 传统版 |
sForum ][登录后回复]6楼
(引用 采心:hey!!! 你怎么干这种事?你这不是还别人吗?ZWB会查的,逮到就死了!!! 害人精!).......是不应该给答案,但也不应该这样地骂。。。[coolapple (1-21 13:35, Long long ago)] [ 传统版 | sForum ][登录后回复]7楼
(引用 采心:hey!!! 你怎么干这种事?你这不是还别人吗?ZWB会查的,逮到就死了!!! 害人精!)lab 0 lah不用紧张[Climbing (1-21 14:55, Long long ago)] [ 传统版 | sForum ][登录后回复]8楼
(引用 coolapple:.......是不应该给答案,但也不应该这样地骂。。。):$管lab的人说,lab0也会查,所以,要么不交,要叫就别交一样的程序,不然,黑黑黑黑!!!:D[采心 (1-21 15:56, Long long ago)] [ 传统版 | sForum ][登录后回复]9楼
(引用 采心::$管lab的人说,lab0也会查,所以,要么不交,要叫就别交一样的程序,不然,黑黑黑黑!!!:D)haha...[不告诉你 (1-21 23:50, Long long ago)] [ 传统版 | sForum ][登录后回复]10楼