2013-09-27 59 views
0

我想弄清楚如何通過給定一個小數String計算有效位數以便我可以對小數進行計算並打印出結果具有相同的有效位數。下面是一個SSCCE:Java - 是否有可能找出一個字符串的DecimalFormat

import java.text.DecimalFormat; 
import java.text.ParseException; 

public class Test { 

    public static void main(String[] args) { 
     try { 
      DecimalFormat df = new DecimalFormat(); 
      String decimal1 = "54.60"; // Decimal is input as a string with a specific number of significant digits. 
      double d = df.parse(decimal1).doubleValue(); 
      d = d * -1; // Multiply the decimal by -1 (this is why we parsed it, so we could do a calculatin). 
      System.out.println(df.format(d)); // I need to print this with the same # of significant digits. 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我知道DecimalFormat是:1)告知程序,你打算怎麼你十進制來顯示(format())和2)告訴程序期望一個字符串表示的十進制是什麼格式在(parse())。但是,有沒有辦法從解析的字符串中減去DecimalFormat,然後使用相同的DecimalFormat來輸出一個數字?

回答

4

使用BigDecimal

 String decimal1 = "54.60"; 
     BigDecimal bigDecimal = new BigDecimal(decimal1); 
     BigDecimal negative = bigDecimal.negate(); // negate keeps scale 
     System.out.println(negative); 

還是短版:

 System.out.println((new BigDecimal(decimal1)).negate()); 
+0

這是一個很好的解決方案。最有用,最準確,最簡單。更不要說'negate()'保持原始尺度的事實,它似乎是在構建'BigDecimal'時有人想到這個問題。 – ryvantage

0
int sigFigs = decimal1.split("\\.")[1].length(); 

計算小數點右邊的字符串長度可能是實現您的目標最簡單的方法。

+2

這是不正確的 - 'split()'需要一個正則表達式,所以'.'匹配任何東西。如果這是固定的,那麼當沒有小數點時它仍然會失敗。說實話,我並不喜歡使用split()做類似PHP的方法。 –

+0

對不起,我修好了。我周圍沒有一個javac ... –

1

通過String.indexOf('.')找到它。

public int findDecimalPlaces (String input) { 
    int dot = input.indexOf('.'); 
    if (dot < 0) 
     return 0; 
    return input.length() - dot - 1; 
} 

也可以通過setMinimumFractionDigits()setMaximumFractionDigits()配置一個DecimalFormat/NumberFormat的設置的輸出格式,而不必建立圖案作爲字符串。

-1

我添加了一個代碼來設置小數位數。如果你想在decimal1

public static void main(String[] args) { 
     try { 
      DecimalFormat df = new DecimalFormat(); 
      String decimal1 = "54.60"; // Decimal is input as a string with a specific number of significant digits. 
      // 
      int index = decimal1.indexOf("."); 
      int prec = -1; 
      if (index != -1) { 
       prec = decimal1.substring(index, decimal1.length()).length(); 
      } 
      if (prec>0) { 
       df.setMaximumFractionDigits(prec); 
       df.setMinimumFractionDigits(prec-1); 
      } 
      // 
      double d = df.parse(decimal1).doubleValue(); 
      d = d * -1; // Multiply the decimal by -1 (this is why we parsed it, so we could do a calculatin). 
      System.out.println(df.format(d)); // I need to print this with the same # of significant digits. 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

@EJP你先運行代碼。它經過測試並提供適當的輸出。你需要通過DecimalFormat的javadoc。 –

0

保持相同小數位,你不能首先使用浮點數,因爲FP沒有他們:FP有二進制的地方。使用BigDecimal,並直接從String.構建它我不明白爲什麼您需要DecimalFormat對象。

0

你可以使用正則表達式的一些字符串轉換爲一個格式字符串:

String format = num.replaceAll("^\\d*", "#").replaceAll("\\d", "0"); 

如 「123.45」 - > 「#.00」 和 「123」 - > 「#」

然後使用結果作爲DecimalFormat的模式。它不僅可以工作,而且只有一行。

相關問題