Sim88 Posted June 28, 2019 Report Share Posted June 28, 2019 Cześć, Jestem nowy na forum i nie mniej nowy w programowaniu. Mam za sobą zaledwie ok. 10 godzin nauki Javy, więc wiem, że nic nie wiem Wykonałem proste ćwiczenie z użyciem pętli: public class Main { public static void main(String[] args) { for (int i=1; i<31; i++) { System.out.println(calculateInterest(10000, i)); } } public static double calculateInterest (double amount, double interestRate) { return (amount * (interestRate/100)); } } Spodziewałem się otrzymać taki wynik: 100.0 200.0 300.0 400.0 500.0 600.0 700.0 800.0 900.0 1000.0 1100.0 1200.0 1300.0 1400.0 1500.0 1600.0 1700.0 1800.0 1900.0 2000.0 2100.0 2200.0 2300.0 2400.0 2500.0 2600.0 2700.0 2800.0 2900.0 3000.0 Tymczasem, ku mojemu zaskoczeniu, komputer zwrócił takie coś: 100.0 200.0 300.0 400.0 500.0 600.0 700.0000000000001 800.0 900.0 1000.0 1100.0 1200.0 1300.0 1400.0000000000002 1500.0 1600.0 1700.0000000000002 1800.0 1900.0 2000.0 2100.0 2200.0 2300.0 2400.0 2500.0 2600.0 2700.0 2800.0000000000005 2900.0 3000.0 Skąd biorą się te ułamki? Quote Link to comment Share on other sites More sharing options...
Bartosz Wójcik Posted January 7, 2020 Report Share Posted January 7, 2020 Chyba masz podkręcony procesor Quote Link to comment Share on other sites More sharing options...
softwareman Posted July 7, 2021 Report Share Posted July 7, 2021 Kwestia precyzji, mozesz np uzyc dodatkowej metody: public static double roundAvoid(double value, int places) { double scale = Math.pow(10, places); return Math.round(value * scale) / scale; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.