kali-hi
Banned
- Registriert
- Sep. 2025
- Beiträge
- 760
Hallo, irgendwie sehe ich den Wald vor lauter Bäumen gerade nicht...
Ich möchte die Menge und den Preis auf zwei Stellen Signifikanz anhand realer Bedingungen runden.
Beispiel:
Code:
Ausgabe:
Bei 4, 5, 6 stimmt die Menge nicht und bei 3, 4, 5, 6, 7 stimmt der Preis nicht.
Sieht vielleicht jemand, was falsch ist? Vorweg: Das sind keine Hausaufgaben.
Ich möchte die Menge und den Preis auf zwei Stellen Signifikanz anhand realer Bedingungen runden.
Beispiel:
| Preis | Menge | Preis neu | gerundet | gerundet |
| 95000.0 | 0.0000526316 | 95380.0 | 0.000053 | 95380.0 |
| 2000.0 | 0.0025 | 2008.0 | 0.0025 | 2008.0 |
| 0.5 | 10.0 | 0.502 | 10.0 | 0.502 |
| 0.07 | 71.4285714286 | 0.07028 | 71.0 | 0.0702 |
| 0.0012 | 4166.6666666667 | 0.0012048 | 4166.0 | 0.001204 |
| 0.000045 | 111111.1111111111 | 0.00004518 | 111111.0 | 0.0000451 |
| 111111.0 | 0.000045 | 111555.444 | 0.000045 | 111555.44 |
Code:
Java:
public static double formatAmount(double price, double amount) {
double rawQuantity = 1.0 / price;
int zeros = numberOfZeros(rawQuantity);
double precision = Math.pow(10, -zeros);
return Math.round(amount / precision) * precision;
}
public static double formatPrice(double old_price, double new_price) {
return formatAmount(old_price, new_price);
}
public static int numberOfZeros(double value) {
String text = String.format(Locale.ENGLISH, "%.10f", value);
int indexOfDecimal = text.indexOf('.');
String integerPart = text.substring(0, indexOfDecimal);
String fractionPart = text.substring(indexOfDecimal + 1);
if (Integer.parseInt(integerPart) == 0) {
int count = 2;
for (char c : fractionPart.toCharArray()) {
if (c == '0') {
count++;
} else {
break;
}
}
return count;
}
return -integerPart.length();
}
public static void testFormatMethods() {
double[][] testValues = {
{95000.0, 5.0 / 95000.0},
{2000.0, 5.0 / 2000.0},
{0.5, 5.0 / 0.5},
{0.07, 5.0 / 0.07},
{0.0012, 5.0 / 0.0012},
{0.000045, 5.0 / 0.000045},
{111111.0, 5.0 / 111111.0}
};
int index = 1;
for (double[] pair : testValues) {
double price = pair[0];
double amount = pair[1];
double rawPrice = price * 1.004;
double formattedAmount = formatAmount(price, amount);
double formattedPrice = formatPrice(price, rawPrice);
System.out.printf(
Locale.ENGLISH,
"%d. Price: %.10f, Raw Amount: %.10f, Raw Price: %.10f => Formatted Amount: %.10f, Formatted Price: %.10f%n",
index++,
price,
amount,
rawPrice,
formattedAmount,
formattedPrice);
}
}
Ausgabe:
Code:
1. Price: 95000.0000000000, Raw Amount: 0.0000526316, Raw Price: 95380.0000000000 => Formatted Amount: 0.0000530000, Formatted Price: 95380.0000000000
2. Price: 2000.0000000000, Raw Amount: 0.0025000000, Raw Price: 2008.0000000000 => Formatted Amount: 0.0025000000, Formatted Price: 2008.0000000000
3. Price: 0.5000000000, Raw Amount: 10.0000000000, Raw Price: 0.5020000000 => Formatted Amount: 10.0000000000, Formatted Price: 0.0000000000
4. Price: 0.0700000000, Raw Amount: 71.4285714286, Raw Price: 0.0702800000 => Formatted Amount: 100.0000000000, Formatted Price: 0.0000000000
5. Price: 0.0012000000, Raw Amount: 4166.6666666667, Raw Price: 0.0012048000 => Formatted Amount: 4000.0000000000, Formatted Price: 0.0000000000
6. Price: 0.0000450000, Raw Amount: 111111.1111111111, Raw Price: 0.0000451800 => Formatted Amount: 100000.0000000000, Formatted Price: 0.0000000000
7. Price: 111111.0000000000, Raw Amount: 0.0000450000, Raw Price: 111555.4440000000 => Formatted Amount: 0.0000450000, Formatted Price: 111555.4440000000
Bei 4, 5, 6 stimmt die Menge nicht und bei 3, 4, 5, 6, 7 stimmt der Preis nicht.
Sieht vielleicht jemand, was falsch ist? Vorweg: Das sind keine Hausaufgaben.