Fixes #1202: Now 10000x items is the limit before being converted to next bigger unit

Added a new method to display a number in a long fashion.
Added respective unit tests for them
This commit is contained in:
thatsIch
2015-04-07 23:46:52 +02:00
parent 2667b8eac6
commit b58c92ebb6
3 changed files with 168 additions and 3 deletions
@@ -129,7 +129,7 @@ public class AppEngRenderItem extends RenderItem
}
else
{
return NUMBER_CONVERTER.toHumanReadableForm( originalSize );
return NUMBER_CONVERTER.toLongHumanReadableForm( originalSize );
}
}
}
@@ -49,7 +49,7 @@ public enum ReadableNumberConverter
final long absNumber = Math.abs( number );
if( absNumber < DIVISION_BASE )
return Long.toString( absNumber );
return Long.toString( number );
final int exp = (int) ( Math.log( absNumber ) / LOG_DIVISION_BASE );
final char postFix = ENCODED_POSTFIXES[exp - 1];
@@ -93,7 +93,7 @@ public enum ReadableNumberConverter
final long absNumber = Math.abs( number );
if( absNumber < DIVISION_BASE )
return Long.toString( absNumber );
return Long.toString( number );
final int exp = (int) ( Math.log( absNumber ) / LOG_DIVISION_BASE );
final int result = (int) ( absNumber / Math.pow( DIVISION_BASE, exp ) );
@@ -111,4 +111,31 @@ public enum ReadableNumberConverter
return String.format( "%s%d%s", sign, result, postFix );
}
}
/**
* Converts a number into a human readable form. It will not round the number, but floor it.
* Will try to cut the number down 1 decimal later.
*
* Example:
* 10000L -> 10K
* 9999L -> 9999
*
* @param number to be converted number
*
* @return String in SI format cut down as far as possible
*/
public String toLongHumanReadableForm( long number )
{
final String sign = this.getSign( number );
final long absNumber = Math.abs( number );
if( absNumber < 10 * DIVISION_BASE )
return Long.toString( number );
final int exp = (int) ( Math.log( absNumber / 10 ) / LOG_DIVISION_BASE );
final int result = (int) ( absNumber / Math.pow( DIVISION_BASE, exp ) );
final char postFix = ENCODED_POSTFIXES[exp - 1];
return String.format( "%s%d%s", sign, result, postFix );
}
}