⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fmt.java

📁 java高级使用教程 全书一共分六章
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    throw new InternalError( "Acme.Fmt: zero-fill left-justify is silly" );
	if ( hexadecimal && octal )
	    throw new InternalError( "Acme.Fmt: can't do both hex and octal" );
	if ( len >= minWidth )
	    return s;
	int fillWidth = minWidth - len;
	StringBuffer fill = new StringBuffer( fillWidth );
	for ( int i = 0; i < fillWidth; ++i )
	    if ( zeroFill )
		fill.append( '0' );
	    else
		fill.append( ' ' );
	if ( leftJustify )
	    return s + fill;
	else if ( zeroFill && s.startsWith( "-" ) )
	    return "-" + fill + s.substring( 1 );
	else
	    return fill + s;
	}


    // Internal routines.

    private static String sigFigFix( String s, int sigFigs )
	{
	// First dissect the floating-point number string into sign,
	// integer part, fraction part, and exponent.
	String sign;
	String unsigned;
	if ( s.startsWith( "-" ) || s.startsWith( "+" ) )
	    {
	    sign = s.substring( 0, 1 );
	    unsigned = s.substring( 1 );
	    }
	else
	    {
	    sign = "";
	    unsigned = s;
	    }
	String mantissa;
	String exponent;
	int eInd = unsigned.indexOf( 'e' );
	if ( eInd == -1 )	// it may be 'e' or 'E'
	    eInd = unsigned.indexOf( 'E' );
	if ( eInd == -1 )
	    {
	    mantissa = unsigned;
	    exponent = "";
	    }
	else
	    {
	    mantissa = unsigned.substring( 0, eInd );
	    exponent = unsigned.substring( eInd );
	    }
	StringBuffer number, fraction;
	int dotInd = mantissa.indexOf( '.' );
	if ( dotInd == -1 )
	    {
	    number = new StringBuffer( mantissa );
	    fraction = new StringBuffer( "" );
	    }
	else
	    {
	    number = new StringBuffer( mantissa.substring( 0, dotInd ) );
	    fraction = new StringBuffer( mantissa.substring( dotInd + 1 ) );
	    }

	int numFigs = number.length();
	int fracFigs = fraction.length();
	if ( ( numFigs == 0 || number.equals( "0" ) ) && fracFigs > 0 )
	    {
	    // Don't count leading zeros in the fraction.
	    numFigs = 0;
	    for ( int i = 0; i < fraction.length(); ++i )
		{
		if ( fraction.charAt( i ) != '0' )
		    break;
		--fracFigs;
		}
	    }
	int mantFigs = numFigs + fracFigs;
	if ( sigFigs > mantFigs )
	    {
	    // We want more figures; just append zeros to the fraction.
	    for ( int i = mantFigs; i < sigFigs; ++i )
		fraction.append( '0' );
	    }
	else if ( sigFigs < mantFigs && sigFigs >= numFigs )
	    {
	    // Want fewer figures in the fraction; chop.
	    fraction.setLength(
		fraction.length() - ( fracFigs - ( sigFigs - numFigs ) ) );
	    // Round?
	    }
	else if ( sigFigs < numFigs )
	    {
	    // Want fewer figures in the number; turn them to zeros.
	    fraction.setLength( 0 );	// should already be zero, but make sure
	    for ( int i = sigFigs; i < numFigs; ++i )
		number.setCharAt( i, '0' );
	    // Round?
	    }
	// Else sigFigs == mantFigs, which is fine.

	if ( fraction.length() == 0 )
	    return sign + number + exponent;
	else
	    return sign + number + "." + fraction + exponent;
	}


    /// Improved version of Double.toString(), returns more decimal places.
    // <P>
    // The JDK 1.0.2 version of Double.toString() returns only six decimal
    // places on some systems.  In JDK 1.1 full precision is returned on
    // all platforms.
    // @deprecated
    // @see java.lang.Double#toString
    public static String doubleToString( double d )
	{
	// Handle special numbers first, to avoid complications.
	if ( Double.isNaN( d ) )
	    return "NaN";
	if ( d == Double.NEGATIVE_INFINITY )
	    return "-Inf";
	if ( d == Double.POSITIVE_INFINITY )
	    return "Inf";

	// Grab the sign, and then make the number positive for simplicity.
	boolean negative = false;
	if ( d < 0.0D )
	    {
	    negative = true;
	    d = -d;
	    }

	// Get the native version of the unsigned value, as a template.
	String unsStr = Double.toString( d );

	// Dissect out the exponent.
	String mantStr, expStr;
	int exp;
	int eInd = unsStr.indexOf( 'e' );
	if ( eInd == -1 )	// it may be 'e' or 'E'
	    eInd = unsStr.indexOf( 'E' );
	if ( eInd == -1 )
	    {
	    mantStr = unsStr;
	    expStr = "";
	    exp = 0;
	    }
	else
	    {
	    mantStr = unsStr.substring( 0, eInd );
	    expStr = unsStr.substring( eInd + 1 );
	    if ( expStr.startsWith( "+" ) )
		exp = Integer.parseInt( expStr.substring( 1 ) );
	    else
		exp = Integer.parseInt( expStr );
	    }

	// Dissect out the number part.
	String numStr;
	int dotInd = mantStr.indexOf( '.' );
	if ( dotInd == -1 )
	    numStr = mantStr;
	else
	    numStr = mantStr.substring( 0, dotInd );
	long num;
	if ( numStr.length() == 0 )
	    num = 0;
	else
	    num = Integer.parseInt( numStr );

	// Build the new mantissa.
	StringBuffer newMantBuf = new StringBuffer( numStr + "." );
	double p = Math.pow( 10, exp );
	double frac = d - num * p;
	String digits = "0123456789";
	int nDigits = 16 - numStr.length();	// about 16 digits in a double
	for ( int i = 0; i < nDigits; ++i )
	    {
	    p /= 10.0D;
	    int dig = (int) ( frac / p );
	    if ( dig < 0 ) dig = 0;
	    if ( dig > 9 ) dig = 9;
	    newMantBuf.append( digits.charAt( dig ) );
	    frac -= dig * p;
	    }

	if ( (int) ( frac / p + 0.5D ) == 1 )
	    {
	    // Round up.
	    boolean roundMore = true;
	    for ( int i = newMantBuf.length() - 1; i >= 0; --i )
		{
		int dig = digits.indexOf( newMantBuf.charAt( i ) );
		if ( dig == -1 )
		    continue;
		++dig;
		if ( dig == 10 )
		    {
		    newMantBuf.setCharAt( i, '0' );
		    continue;
		    }
		newMantBuf.setCharAt( i, digits.charAt( dig ) );
		roundMore = false;
		break;
		}
	    if ( roundMore )
		{
		// If this happens, we need to prepend a 1.  But I haven't
		// found a test case yet, so I'm leaving it out for now.
		// But if you get this message, please let me know!
		newMantBuf.append( "ROUNDMORE" );
		}
	    }

	// Chop any trailing zeros.
	int len = newMantBuf.length();
	while ( newMantBuf.charAt( len - 1 ) == '0' )
	    newMantBuf.setLength( --len );
	// And chop a trailing dot, if any.
	if ( newMantBuf.charAt( len - 1 ) == '.' )
	    newMantBuf.setLength( --len );

	// Done.
	return ( negative ? "-" : "" ) +
	       newMantBuf +
	       ( expStr.length() != 0 ? ( "e" + expStr ) : "" );
	}


/******************************************************************************
    /// Test program.
    public static void main( String[] args )
	{
	System.out.println( "Starting tests." );
	show( Fmt.fmt( "Hello there." ) );
	show( Fmt.fmt( 123 ) );
	show( Fmt.fmt( 123, 10 ) );
	show( Fmt.fmt( 123, 10, Fmt.ZF ) );
	show( Fmt.fmt( 123, 10, Fmt.LJ ) );
	show( Fmt.fmt( -123 ) );
	show( Fmt.fmt( -123, 10 ) );
	show( Fmt.fmt( -123, 10, Fmt.ZF ) );
	show( Fmt.fmt( -123, 10, Fmt.LJ ) );
	show( Fmt.fmt( (byte) 0xbe, 22, Fmt.OC ) );
	show( Fmt.fmt( (short) 0xbabe, 22, Fmt.OC ) );
	show( Fmt.fmt( 0xcafebabe, 22, Fmt.OC ) );
	show( Fmt.fmt( 0xdeadbeefcafebabeL, 22, Fmt.OC ) );
	show( Fmt.fmt( 0x8000000000000000L, 22, Fmt.OC ) );
	show( Fmt.fmt( (byte) 0xbe, 16, Fmt.HX ) );
	show( Fmt.fmt( (short) 0xbabe, 16, Fmt.HX ) );
	show( Fmt.fmt( 0xcafebabe, 16, Fmt.HX ) );
	show( Fmt.fmt( 0xdeadbeefcafebabeL, 16, Fmt.HX ) );
	show( Fmt.fmt( 0x8000000000000000L, 16, Fmt.HX ) );
	show( Fmt.fmt( 'c' ) );
	show( Fmt.fmt( new java.util.Date() ) );
	show( Fmt.fmt( 123.456F ) );
	show( Fmt.fmt( 123456000000000000.0F ) );
	show( Fmt.fmt( 123.456F, 0, 8 ) );
	show( Fmt.fmt( 123.456F, 0, 7 ) );
	show( Fmt.fmt( 123.456F, 0, 6 ) );
	show( Fmt.fmt( 123.456F, 0, 5 ) );
	show( Fmt.fmt( 123.456F, 0, 4 ) );
	show( Fmt.fmt( 123.456F, 0, 3 ) );
	show( Fmt.fmt( 123.456F, 0, 2 ) );
	show( Fmt.fmt( 123.456F, 0, 1 ) );
	show( Fmt.fmt( 123456000000000000.0F, 0, 4 ) );
	show( Fmt.fmt( -123.456F, 0, 4 ) );
	show( Fmt.fmt( -123456000000000000.0F, 0, 4 ) );
	show( Fmt.fmt( 123.0F ) );
	show( Fmt.fmt( 123.0D ) );
	show( Fmt.fmt( 1.234567890123456789F ) );
	show( Fmt.fmt( 1.234567890123456789D ) );
	show( Fmt.fmt( 1234567890123456789F ) );
	show( Fmt.fmt( 1234567890123456789D ) );
	show( Fmt.fmt( 0.000000000000000000001234567890123456789F ) );
	show( Fmt.fmt( 0.000000000000000000001234567890123456789D ) );
	show( Fmt.fmt( 12300.0F ) );
	show( Fmt.fmt( 12300.0D ) );
	show( Fmt.fmt( 123000.0F ) );
	show( Fmt.fmt( 123000.0D ) );
	show( Fmt.fmt( 1230000.0F ) );
	show( Fmt.fmt( 1230000.0D ) );
	show( Fmt.fmt( 12300000.0F ) );
	show( Fmt.fmt( 12300000.0D ) );
	show( Fmt.fmt( Float.NaN ) );
	show( Fmt.fmt( Float.POSITIVE_INFINITY ) );
	show( Fmt.fmt( Float.NEGATIVE_INFINITY ) );
	show( Fmt.fmt( Double.NaN ) );
	show( Fmt.fmt( Double.POSITIVE_INFINITY ) );
	show( Fmt.fmt( Double.NEGATIVE_INFINITY ) );
	show( Fmt.fmt( 1.0F / 8.0F ) );
	show( Fmt.fmt( 1.0D / 8.0D ) );
	System.out.println( "Done with tests." );
	}

    private static void show( String str )
	{
	System.out.println( "#" + str + "#" );
	}
******************************************************************************/

    }

⌨️ 快捷键说明

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