2014-09-17 2 views
0

자릿수를 소수점 이하 자릿수로 가져 오는 데 문제가 있습니다. 나는 소수점의 오른쪽에있는 자리수를 가지고 있으며, 왼쪽으로는 인쇄 할 수 없다. 누구든지 도와 줄 수 있습니까?자릿수를 소수 자릿수의 왼쪽과 오른쪽에 인쇄하십시오.

import java.lang.Math; 
import java.util.Scanner; 
import java.text.DecimalFormat; 

public class FormulaCalculation 
{ 
    public static void main(String[] args) 
    { 
    Scanner userInput = new Scanner(System.in); 
    double x; 

    //Prompt user for value. 
    System.out.print("Enter a value for x: "); 
    x = userInput.nextDouble(); 


    double result = (Math.sqrt(7 * Math.pow(x, 4) - 5 * Math.pow(x, 2) + Math.abs(98 * x)) + 13) * (3 * Math.pow(x, 5) + 4 * Math.pow(x, 3) + 1); 
    String resultString = Double.toString(result); 


    int integerPlaces = resultString.indexOf('.'); 
    int digitsLeft = resultString.length(); 
    int digitsRight = resultString.length() - integerPlaces - 1; 


    //Output 
    System.out.println("Result: " + result); 
    System.out.println("# digits to left of the decimal point: " + digitsLeft); 
    System.out.println("# digits to right of the decimal point: " + digitsRight); 
    System.out.println("Formatted Result: "); 
    } 

} 
+0

은 * 내가 아니라 그것이 올바른 *에 근무하는 소수점의 오른쪽에있는 숫자를 가지고

int integerPlaces = resultString.indexOf('.'); int digitsLeft = integerPlaces; // <-- from 0 to the '.' // resultString.length(); int digitsRight = resultString.length() - integerPlaces - 1; 

또는 전부 digitsLeft을 제거, 이런 식으로 뭔가? –

+1

해결되었습니다. 나는 정확하게 인쇄하기 위해 왼쪽으로 자릿수를 얻을 수 없다는 것을 의미했다. – collint25

답변

0

어느 인쇄 integerPlaces하거나 digitsLeft에 할당합니다 (당신이 찾는 번호입니다).

System.out.println("# digits to left of the decimal point: " + integerPlaces); 
+0

감사합니다. – collint25

관련 문제