Pozbywanie się końcowych zer przy zaokrąglaniu liczby typu Double

0

Witam. Kolejny raz podczas swoich początkowch strać z javą napotykam na drobny problem i nie umiem sobie z nim poradzić w prosty sposób, a mianowicie pozbywanie się zer.

actualOutput: 55.5000 83.5324, a chciałbym uzyskać coś takiego:
desirableOutput: 55.5 83.5324

Innymi słowy, żeby liczba która ma np. 5 miejsc po przecinku miała to swoje 5 miejsc po przecinku, a liczba ktora wychodzi 55.5 niech tak wyglada.

Przy printf %.liczba miejscf nie działa, tak samo jak klasa DecimalFormat, poniewaz nie wiem jaka wyjdzie mi liczba wiec nie moge jej sformatowac, potrzebuje czegos uniwersalnego, bo bawienie sie w castowanie tego na string i obcinanie zer nie bardzo mnie zadowala.

//package ex41; 

import java.util.Scanner;

public class Grade {

	public static void main(String[] args) {		
		int scores[][] = new int[2][2];
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("Please enter your math and science scores for 9th grade: ");
		String scoreLine = scanner.nextLine();
		
		//split the string to get an array of Strings which will  
		//hold the values entered by the user 
		String strScores[] = scoreLine.split(" ");
		
		//convert the first number (which represents the score for math in 9th grade)  
		//from a String to an integer and store it in scores[0][0] 
		scores[0][0] = Integer.parseInt(strScores[0].trim());
		//convert the second number (which represents the score for science in 9th grade)  
		//from a String to an integer and store it in scores[0][1] 
		scores[0][1] = Integer.parseInt(strScores[1].trim());
		
		System.out.println("Please enter your math and science scores for 10th grade: ");
		scoreLine = scanner.nextLine();
		
		//split the string to get an array of Strings which will  
		//hold the values entered by the user 
		strScores = scoreLine.split(" ");
		
		//convert the first number (which represents the score for math in 10th grade)  
		//from a String to an integer and store it in scores[1][0] 
		scores[1][0] = Integer.parseInt(strScores[0].trim());
		//convert the second number (which represents the score for science in 10th grade)  
		//from a String to an integer and store it in scores[1][1] 
		scores[1][1] = Integer.parseInt(strScores[1].trim());
		
		System.out.println("The average scores for math and science are: ");
		
		///{ 
		//write your code here 
              //start 
        double mathAverage = (scores[0][0]+scores[1][0])/2;
        double sciAverage = ((double)scores[0][1]+scores[1][1])/2;
	    System.out.printf("Average Math Score Is: %f\nAverage Science Sore Is: %f", mathAverage, sciAverage);
    
    

               //end 
		///} 
	}

}
1

System.out.println(new BigDecimal(Double.toString(myDoubleNumber)).stripTrailingZeros().toPlainString());

0

new BigDecimal - co to dokładnie zrobiło? Bo dalej to domyślam się, że po prostu korzystamy z metod z klasy BigDecimal itd, ale pierwszy raz sie spotkałem ze słówkiem new xxx w Sysout.

0

A co jest złego w klasie DecimalFormat?

DecimalFormat df = new DecimalFormat("###.#################");
System.out.println(df.format(myDoubleNumber));
0
bogdans napisał(a):

A co jest złego w klasie DecimalFormat?

DecimalFormat df = new DecimalFormat("###.#################");
System.out.println(df.format(myDoubleNumber));

To: ###.################# lub w innym wariancie, to: df.setMaximumFractionDigits(jakaśWartość);, no i może jeszcze to, że podoba mi się moje rozwiązanie :)

0

A jak Twoim sposobem uzyskać jednocześnie, obcinanie końcowych zer i wyświetlanie najwyżej 4 cyfr po przecinku?
W moim sposobie jest to proste

DecimalFormat df = new DecimalFormat("#.####");
0

A z jakim problemem borykał się założyciel tego tematu?

0

Po pierwsze, co to ma do rzeczy?
Po drugie, nie wiemy.

0
bogdans napisał(a)

A jak Twoim sposobem uzyskać jednocześnie, obcinanie końcowych zer i wyświetlanie najwyżej 4 cyfr po przecinku?

Skoro wynik i tak ma wylądować na ekranie konsoli, to wynik konwersji z zaokrągleniem można potraktować jako string, a następnie usunąć zera końcowe (jeśli istnieją) i tak przygotowany łańcuch wyświetlić w konsoli;

Skoro nie zrobi się tego jedną instrukcją, to trzeba kilkoma.

0
bogdans napisał(a):

Po pierwsze, co to ma do rzeczy?
Po drugie, nie wiemy.

Ja w swoim rozwiązaniu założyłem, że właściciel tematu potrzebuje uniwersalnego rozwiązania, które obetnie wszystkie końcowe zera dla liczby o nieokreślonej liczbie wartości niezerowych po przecinku. Jeżeli szukałby rozwiązania, które musi ucinać zera i dodatkowo zaokrąglać liczbę dla określonej ilości miejsc po przecinku, to zapewne poleciłbym DecimalFormat.

1 użytkowników online, w tym zalogowanych: 0, gości: 1