gformat.java

来自「一个用JAVA 写的程序」· Java 代码 · 共 45 行

JAVA
45
字号
import java.text.*;

public class GFormat
//a class holding static methods for 
//formatting purposes
{
	public static String padRight(String s, int n)
	//if s has fewer than n characters then 
	//adds enough spaces to the right of s to make up to n
	{
		String ns =s;
		while (ns.length()<n)
			ns=ns+' ';
		return ns;
	}
	
	public static String padLeft(String s, int n)	
	//if s has fewer than n characters then 
	//adds enough spaces to the left of s to make up to n
	{
		String ns =s;
		while (ns.length()<n)
			ns=' '+ns;
		return ns;
	}
	
	public static String padLeftZeroes(int num, int n)
	{
		String ns ="" + num;
		while (ns.length()<n)
			ns='0'+ns;
		return ns;	
	}
	
	public static String fixDecimalPlaces(double d, int n)
	//returns a String representation of d rounded to 
	//n decimal places.  If necessary 0's are added on right
	{
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(n);
		nf.setMinimumFractionDigits(n);
		return nf.format(d);
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?