mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-18 19:56:17 -04:00
Add more precision support to format
Add precision support to e,E,f Add alternateForm support for e,E,f,g,G
This commit is contained in:
parent
b22cfc4817
commit
bc46f8ca66
@ -353,13 +353,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
public final int conversion;
|
public final int conversion;
|
||||||
public final int length;
|
public final int length;
|
||||||
|
|
||||||
private DecimalFormat scientificFormat;
|
|
||||||
private DecimalFormat floatingFormat;
|
|
||||||
|
|
||||||
public FormatDesc(Varargs args, LuaString strfrmt, final int start) {
|
public FormatDesc(Varargs args, LuaString strfrmt, final int start) {
|
||||||
// TODO: force positive sign
|
|
||||||
scientificFormat = new DecimalFormat("0.000000E00");
|
|
||||||
floatingFormat = new DecimalFormat("0.000000");
|
|
||||||
int p = start, n = strfrmt.length();
|
int p = start, n = strfrmt.length();
|
||||||
int c = 0;
|
int c = 0;
|
||||||
|
|
||||||
@ -514,13 +508,18 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void format(Buffer buf, double number) {
|
public void format(Buffer buf, double number) {
|
||||||
// TODO: precision, alternateForm
|
// TODO: force positive sign
|
||||||
|
|
||||||
|
int precise = precision == -1 ? 6 : precision;
|
||||||
|
String addDot = alternateForm || precise > 0 ? "." : "";
|
||||||
|
|
||||||
|
DecimalFormat scientificFormat = new DecimalFormat("0" + addDot + new String(new char[precise]).replace("\0", "0") + "E00");
|
||||||
|
DecimalFormat floatingFormat = new DecimalFormat("0" + addDot + new String(new char[precise]).replace("\0", "0"));
|
||||||
|
|
||||||
String digits;
|
String digits;
|
||||||
|
|
||||||
switch ( conversion ) {
|
switch ( conversion ) {
|
||||||
case 'e':
|
case 'e':
|
||||||
digits = scientificFormat.format( number ).toLowerCase();
|
|
||||||
break;
|
|
||||||
case 'E':
|
case 'E':
|
||||||
digits = scientificFormat.format( number );
|
digits = scientificFormat.format( number );
|
||||||
break;
|
break;
|
||||||
@ -530,11 +529,15 @@ public class StringLib extends TwoArgFunction {
|
|||||||
case 'g':
|
case 'g':
|
||||||
case 'G':
|
case 'G':
|
||||||
default:
|
default:
|
||||||
//TODO: g, G
|
// TODO: g, G
|
||||||
|
// TODO: precision
|
||||||
digits = String.valueOf( number );
|
digits = String.valueOf( number );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( conversion == 'e' || conversion == 'g' )
|
||||||
|
digits = digits.toLowerCase();
|
||||||
|
|
||||||
int minwidth = digits.length();
|
int minwidth = digits.length();
|
||||||
int ndigits = minwidth;
|
int ndigits = minwidth;
|
||||||
int nzeros;
|
int nzeros;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user