implement Float/Double toString
This commit is contained in:
parent
9677332560
commit
aa09b6d916
@ -1,7 +1,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "printf.h"
|
||||
#include "frame.h"
|
||||
#include "class_resolver.h"
|
||||
#include "string.h"
|
||||
@ -13,7 +13,7 @@ static struct hash_table_entry * load_from_filenames(const char * filenames[], i
|
||||
size_t file_size[length];
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
printf("load class: %s\n", filenames[i]);
|
||||
debugf("load class: %s\n", filenames[i]);
|
||||
buffers[i] = file_read(filenames[i], &file_size[i]);
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,30 @@
|
||||
package java.lang;
|
||||
|
||||
public class Double {
|
||||
public static String toString(double d) {
|
||||
return "";
|
||||
public static String toString(double f) {
|
||||
long integer = (long)f;
|
||||
double frac = (f - (double)integer) * 10000.0;
|
||||
long fraction = (long)frac;
|
||||
|
||||
byte[] int_s = Long.toString(integer).getBytes();
|
||||
byte[] frac_s = Long.toString(fraction).getBytes();
|
||||
int length = int_s.length + frac_s.length + 1;
|
||||
byte[] buf = new byte[length];
|
||||
|
||||
int ix = 0;
|
||||
for (int i = 0; i < int_s.length; i++) {
|
||||
buf[ix] = int_s[i];
|
||||
ix += 1;
|
||||
}
|
||||
|
||||
buf[ix] = (byte)'.';
|
||||
ix += 1;
|
||||
|
||||
for (int i = 0; i < frac_s.length; i++) {
|
||||
buf[ix] = frac_s[i];
|
||||
ix += 1;
|
||||
}
|
||||
|
||||
return new String(buf);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,29 @@ package java.lang;
|
||||
|
||||
public class Float {
|
||||
public static String toString(float f) {
|
||||
return "";
|
||||
int integer = (int)f;
|
||||
float frac = (f - (float)integer) * 10000.0f;
|
||||
int fraction = (int)frac;
|
||||
|
||||
byte[] int_s = Integer.toString(integer).getBytes();
|
||||
byte[] frac_s = Integer.toString(fraction).getBytes();
|
||||
int length = int_s.length + frac_s.length + 1;
|
||||
byte[] buf = new byte[length];
|
||||
|
||||
int ix = 0;
|
||||
for (int i = 0; i < int_s.length; i++) {
|
||||
buf[ix] = int_s[i];
|
||||
ix += 1;
|
||||
}
|
||||
|
||||
buf[ix] = (byte)'.';
|
||||
ix += 1;
|
||||
|
||||
for (int i = 0; i < frac_s.length; i++) {
|
||||
buf[ix] = frac_s[i];
|
||||
ix += 1;
|
||||
}
|
||||
|
||||
return new String(buf);
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,10 @@ public class String {
|
||||
return this;
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return value.length;
|
||||
}
|
||||
|
||||
public static String valueOf(boolean b) {
|
||||
return b ? "true" : "false";
|
||||
}
|
||||
|
11
p/TestFloatPrint.java
Normal file
11
p/TestFloatPrint.java
Normal file
@ -0,0 +1,11 @@
|
||||
package p;
|
||||
|
||||
class TestFloatPrint {
|
||||
public static void main() {
|
||||
float f = 1234.5678f;
|
||||
System.out.println(f);
|
||||
|
||||
double d = 1234.5678;
|
||||
System.out.println(d);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user