1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

support for ToString(X2) and number.round(places)

This commit is contained in:
Kevin Glynn 2010-07-14 15:13:05 -05:00
parent 2d332eb986
commit aa8dd21ace
2 changed files with 99 additions and 0 deletions

View File

@ -20,6 +20,20 @@
</Imports>
<Java>NumberFormat.getInstance(${provider}).format(${this})</Java>
</Method>
<Method>
<Return>System.String</Return>
<Name>ToString</Name>
<Params>
<Param>
<Type>System.String</Type>
<Name>format</Name>
</Param>
</Params>
<Imports>
<Import>RusticiSoftware.System.NumberSupport</Import>
</Imports>
<Java>NumberSupport.format(${this}, ${format})</Java>
</Method>
</Methods>
<Casts>
</Casts>

View File

@ -0,0 +1,85 @@
/*
Copyright 2007-2010 Rustici Software, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author(s):
Kevin Glynn (kevin.glynn@scorm.com)
*/
package RusticiSoftware.System;
public class NumberSupport {
// public static float round(float num, int places) {
// long factor = (long)Math.pow(10,places);
// return Math.round(num * factor) / factor;
// }
/**
* Round a double value to a specified number of decimal
* places.
*
* @param val the value to be rounded.
* @param places the number of decimal places to round to.
* @return val rounded to places decimal places.
*/
public static double round(double val, int places) {
long factor = (long)Math.pow(10,places);
// Shift the decimal the correct number of places
// to the right.
val = val * factor;
// Round to the nearest integer.
long tmp = Math.round(val);
// Shift the decimal the correct number of places
// back to the left.
return (double)tmp / factor;
}
/**
* Format a number
*
* @param n the value to be formatted.
* @param format a C# standard numeric format string.
* @return n formatted according to C# format string.
* @throws NotImplementedException
*/
public static String format(Number n, String format) throws NotImplementedException {
String fStr = null;
switch (format.charAt(0)){
case 'X':
case 'x':
fStr = String.format("%1$0" + format.substring(1) + format.charAt(0), n);
break;
default:
throw new NotImplementedException("No implementation for " + format + " format string");
}
return fStr;
}
public static void main(String args[]) throws NotImplementedException {
double f = 10.123456789d;
System.out.println(f + " turns into " + round(f,3));
for (int i = 0; i <= 255; i++) {
byte b = (byte)i;
System.out.println(b + " turns into " + format(b,"X2"));
}
}
}