/** Place a fixed width representation into the string buffer : never signed. */ public static void formatUnsignedInt(StringBuilder sb, int value, int width) { char chars[] = new char[width] ; formatUnsignedInt$(chars, value, width) ; // Append - the buffer was filled backwards. for ( int i = 0 ; i < width ; i++ ) // Un-backwards. sb.append(chars[width-1-i]) ; }
/** Place a fixed width representation of a non-negative int into the string buffer */ public static void formatInt(StringBuilder sb, int value, int width) { formatInt(sb, value, width, false) ; }
NumberUtils.formatInt(sb, years, 4); sb.append('-'); NumberUtils.formatInt(sb, months, 2); sb.append('-'); NumberUtils.formatInt(sb, days, 2); if ( isDateTime ) { sb.append('T'); NumberUtils.formatInt(sb, hours, 2); sb.append(':'); NumberUtils.formatInt(sb, minutes, 2); sb.append(':'); NumberUtils.formatInt(sb, sec, 2); sb.append("."); if ( fractionSec % 100 == 0 ) NumberUtils.formatInt(sb, fractionSec / 100, 1); else if ( fractionSec % 10 == 0 ) NumberUtils.formatInt(sb, fractionSec / 10, 2); else NumberUtils.formatInt(sb, fractionSec, 3); NumberUtils.formatUnsignedInt(sb, tzH, 2); sb.append(':'); NumberUtils.formatUnsignedInt(sb, tzM, 2); return sb.toString();
/** Format an integer, which may be signed */ public static void formatInt(StringBuilder sb, int value, int width, boolean signAlways) { boolean negative = (value < 0 ) ; if ( negative ) { value = -value ; width -- ; sb.append('-') ; } else if ( signAlways ) { width -- ; sb.append('+') ; } formatUnsignedInt(sb, value, width) ; }
static int length(int x) { if ( x < 0 ) return length(-x)+1 ; for (int i=0; ; i++) if (x <= maxTable[i]) return i+1; }
private static void testSigned(int value, int width, String expected) { StringBuilder sb = new StringBuilder() ; NumberUtils.formatSignedInt(sb, value, width) ; String result = sb.toString(); assertEquals(expected, result) ; }
/** Place a fixed width representation into the string buffer : always signed. */ public static void formatSignedInt(StringBuilder sb, int value, int width) { formatInt(sb, value, width, true) ; }
NumberUtils.formatInt(sb, years, 4) ; sb.append('-') ; NumberUtils.formatInt(sb, months, 2) ; sb.append('-') ; NumberUtils.formatInt(sb, days, 2) ; if ( isDateTime ) NumberUtils.formatInt(sb, hours, 2) ; sb.append(':') ; NumberUtils.formatInt(sb, minutes, 2) ; sb.append(':') ; NumberUtils.formatInt(sb, sec, 2) ; NumberUtils.formatInt(sb, fractionSec/100, 1) ; else if ( fractionSec%10 == 0 ) NumberUtils.formatInt(sb, fractionSec/10, 2) ; else NumberUtils.formatInt(sb, fractionSec, 3) ; NumberUtils.formatUnsignedInt(sb, tzH, 2) ; sb.append(':') ; NumberUtils.formatUnsignedInt(sb, tzM, 2) ; return sb.toString();
/** Format an integer, which may be signed */ public static void formatInt(StringBuilder sb, int value, int width, boolean signAlways) { boolean negative = (value < 0 ) ; if ( negative ) { value = -value ; width -- ; sb.append('-') ; } else if ( signAlways ) { width -- ; sb.append('+') ; } formatUnsignedInt(sb, value, width) ; }
static int length(int x) { if ( x < 0 ) return length(-x)+1 ; for (int i=0; ; i++) if (x <= maxTable[i]) return i+1; }
/** Place a fixed width representation of a non-negative int into the string buffer */ public static void formatInt(StringBuilder sb, int value, int width) { formatInt(sb, value, width, false) ; }
/** Format an integer, which may be signed */ public static void formatInt(StringBuilder sb, int value, int width, boolean signAlways) { boolean negative = (value < 0 ) ; if ( negative ) { value = -value ; width -- ; sb.append('-') ; } else if ( signAlways ) { width -- ; sb.append('+') ; } formatUnsignedInt(sb, value, width) ; }
/** Place a fixed width representation into the string buffer : never signed. */ public static void formatUnsignedInt(StringBuilder sb, int value, int width) { char chars[] = new char[width] ; formatUnsignedInt$(chars, value, width) ; // Append - the buffer was filled backwards. for ( int i = 0 ; i < width ; i++ ) // Un-backwards. sb.append(chars[width-1-i]) ; }
static int length(int x) { if ( x < 0 ) return length(-x)+1 ; for (int i=0; ; i++) if (x <= maxTable[i]) return i+1; }
/** Place a fixed width representation into the string buffer : always signed. */ public static void formatSignedInt(StringBuilder sb, int value, int width) { formatInt(sb, value, width, true) ; }
private static void testUnsigned(int value, int width, String expected) { StringBuilder sb = new StringBuilder() ; NumberUtils.formatUnsignedInt(sb, value, width) ; String result = sb.toString(); assertEquals(expected, result) ; }
/** Place a fixed width representation into the string buffer : never signed. */ public static void formatUnsignedInt(StringBuilder sb, int value, int width) { char chars[] = new char[width] ; formatUnsignedInt$(chars, value, width) ; // Append - the buffer was filled backwards. for ( int i = 0 ; i < width ; i++ ) // Un-backwards. sb.append(chars[width-1-i]) ; }