Topic: [求助]怎么控制输出格式?

  Print this page

1.[求助]怎么控制输出格式? Copy to clipboard
Posted by: mitnickcbc
Posted on: 2002-12-12 12:58

要输出
1 5 8
100 23 54
24 46 2
如何控制输出内容的宽度?

2.please elaborate your question [Re: mitnickcbc] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-12 13:17

Please specify what exactly you want with the width of the output.

You can also check out the java.text package, it contains NumberFormat class to help you do the format.

3.Re:please elaborate your question [Re: snowbug] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2002-12-12 13:47

snowbug wrote:
Please specify what exactly you want with the width of the output.



How? 请举个例子,输出宽度为10的“abc"。

4.Re:[求助]怎么控制输出格式? [Re: mitnickcbc] Copy to clipboard
Posted by: lucky_sesame
Posted on: 2002-12-12 19:17

据我所知在JDK 1.3 中控制输出宽度能力很弱
试试这个

<a href="http://sharkysoft.com/software/java/printf/">http://sharkysoft.com/software/java/printf/</a>

我刚来,还没有积分,这么多好书都下载不了,我哭....呜... 呜...呜 Sad

5.Re:[求助]怎么控制输出格式? [Re: mitnickcbc] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2002-12-12 19:28

谢谢!
唉,没想到java这方面这么弱。

6.Re:[求助]怎么控制输出格式? [Re: lucky_sesame] Copy to clipboard
Posted by: Biubiu
Posted on: 2002-12-12 21:20

lucky_sesame wrote:
我刚来,还没有积分,这么多好书都下载不了,我哭....呜... 呜...呜 Sad


Oh, that doesn't help, guy. Take care of yourself.

7.It's OK for formatting numbers [Re: mitnickcbc] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-12 22:51

I think it is relatively easy and powerful to format numbers, although the format of string literals is a little weak (but I don't think it is used a lot in formatting strings).

Here is a code sniplet that formats a double number:

DecimalFormat df = new DecimalFormat("$000,000.0000");
String s = df.format(1234.329); //$001, 234.3290
String s2 = df.format(39185129835.31); //$39, 185, 129, 835.3100

Please refer to the DecimalFormat API for details on the usage of patterns

8.Re:[求助]怎么控制输出格式? [Re: mitnickcbc] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2002-12-13 01:06

谢谢!
看来凭我现在的知识是搞不明白怎么回事了。
又是package,又是API,
只能过几天再研究这个问题了。

9.Re:[求助]怎么控制输出格式? [Re: mitnickcbc] Copy to clipboard
Posted by: floater
Posted on: 2002-12-13 01:51

mitnickcbc wrote:
谢谢!
唉,没想到java这方面这么弱。


Actually, it's quite powerful now, Smile.

10.Re:[求助]怎么控制输出格式? [Re: floater] Copy to clipboard
Posted by: floater
Posted on: 2002-12-14 00:35

floater wrote:
Actually, it's quite powerful now, :).


Here are some examples:

import java.text.DecimalFormat;

public class Formatter
{
public static void main(String[] args)
{
//format double
double a = 12314958.123548d;
DecimalFormat df = new DecimalFormat("#0.000");
System.out.println("a=" + df.format(a));

//format string
String format1 = "0000000000";
System.out.println("10 digits with prepending 0's = [" + leftAppend(format1, "1234") + "]");
String format2 = " ";
System.out.println("10 alphanum with prepending spaces = [" + leftAppend(format2, "abc") + "]");
}

/**
* @param format The "background"
* @param s format target
* @return formatted or unchanged if s is larger than format
*/
public static String leftAppend(String format, String s)
{
if (s.length()<format.length())
{
return format.substring(s.length()) + s;
}
return s;
}
}

11.Re:[求助]怎么控制输出格式? [Re: mitnickcbc] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2002-12-14 01:31

太感谢了!Smile

12.Re:[求助]怎么控制输出格式? [Re: mitnickcbc] Copy to clipboard
Posted by: floater
Posted on: 2002-12-15 01:17

Thanks, rainman, for editing the code back to the original.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923