implement Float/Double toString

This commit is contained in:
Zack Buhman 2024-12-29 13:11:25 -06:00
parent 9677332560
commit aa09b6d916
5 changed files with 66 additions and 5 deletions

View File

@ -1,7 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include "printf.h"
#include "frame.h" #include "frame.h"
#include "class_resolver.h" #include "class_resolver.h"
#include "string.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]; size_t file_size[length];
for (int i = 0; i < length; i++) { 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]); buffers[i] = file_read(filenames[i], &file_size[i]);
} }

View File

@ -1,7 +1,30 @@
package java.lang; package java.lang;
public class Double { public class Double {
public static String toString(double d) { public static String toString(double f) {
return ""; 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);
} }
} }

View File

@ -2,6 +2,29 @@ package java.lang;
public class Float { public class Float {
public static String toString(float f) { 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);
} }
} }

View File

@ -23,6 +23,10 @@ public class String {
return this; return this;
} }
public int length() {
return value.length;
}
public static String valueOf(boolean b) { public static String valueOf(boolean b) {
return b ? "true" : "false"; return b ? "true" : "false";
} }

11
p/TestFloatPrint.java Normal file
View 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);
}
}