Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,34 @@
|
|||
double fractionToDecimal(String string) {
|
||||
int indexOf = string.indexOf(' ');
|
||||
int integer = 0;
|
||||
int numerator, denominator;
|
||||
if (indexOf != -1) {
|
||||
integer = Integer.parseInt(string.substring(0, indexOf));
|
||||
string = string.substring(indexOf + 1);
|
||||
}
|
||||
indexOf = string.indexOf('/');
|
||||
numerator = Integer.parseInt(string.substring(0, indexOf));
|
||||
denominator = Integer.parseInt(string.substring(indexOf + 1));
|
||||
return integer + ((double) numerator / denominator);
|
||||
}
|
||||
|
||||
String decimalToFraction(double value) {
|
||||
String string = String.valueOf(value);
|
||||
string = string.substring(string.indexOf('.') + 1);
|
||||
int numerator = Integer.parseInt(string);
|
||||
int denominator = (int) Math.pow(10, string.length());
|
||||
int gcf = gcf(numerator, denominator);
|
||||
if (gcf != 0) {
|
||||
numerator /= gcf;
|
||||
denominator /= gcf;
|
||||
}
|
||||
int integer = (int) value;
|
||||
if (integer != 0)
|
||||
return "%d %d/%d".formatted(integer, numerator, denominator);
|
||||
return "%d/%d".formatted(numerator, denominator);
|
||||
}
|
||||
|
||||
int gcf(int valueA, int valueB) {
|
||||
if (valueB == 0) return valueA;
|
||||
else return gcf(valueB, valueA % valueB);
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import org.apache.commons.math3.fraction.BigFraction;
|
||||
|
||||
public class Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
double[] n = {0.750000000, 0.518518000, 0.905405400, 0.142857143,
|
||||
3.141592654, 2.718281828, -0.423310825, 31.415926536};
|
||||
|
||||
for (double d : n)
|
||||
System.out.printf("%-12s : %s%n", d, new BigFraction(d, 0.00000002D, 10000));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue