簡體   English   中英

從JTable解析的雙精度數之和

[英]Sum of Doubles that where parsed from a JTable

如何獲得從JTable解析的Doubles的總和?

我正在構建一個客戶端/服務器應用程序,而我陷入了一個小問題。

我想獲取JTable中一列的值的總和,此列包含具有#,##格式的String對象,因此當我嘗試使用循環將它們解析為雙精度時,會出現此錯誤:

java.lang.NumberFormatException: For input string: "0,55"

這是JTable圖像:

在此輸入圖像描述

這是我到目前為止嘗試的代碼:

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt)    {                                          

    Double summl = 0.0;

    for(int i = 0; i < jTable1.getRowCount(); i++){
        summl=summl+Double.parseDouble(jTable1.getValueAt(i,5).toString());
    }
     System.out.println("summl = "+summl);
   }  

我認為問題在於數字中的逗號。 您應該嘗試使用NumberFormat類解析數字。

例如:

public static void main(String[] args) {
        NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH); //Set the proper locale here

        try {
            Double myDouble = (Double)nf.parse("0,55");
            System.out.println(myDouble);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM