如何在Java中将长数据类型转换为double数据类型?

时间:2013-03-01 07:40:05

标签: java android

(isPlusClicked || op =='+'){

                long result = 0;
                String finaldata = edt.getText().toString();
                finaldata = finaldata.replace("(", "");
                finaldata = finaldata.replace(")", "");
                System.out.println(" the string is now ==== "+edt.getText().toString());

                String[] total = finaldata.split("\\+");
                System.out.println(" *************** "+total[0] + "************** "+total[1]);
                System.out.println(" the index in the string array are ..... "+sb.toString());
                ArrayList<String> alvalue = new ArrayList<String>();

                System.out.println(" the splited number is ==== "+total[0] +" the second number is "+total[1]);

                StringBuilder sb1 = new StringBuilder(edt.getText().toString());

                int inc = 0;
                for(int i = 0;i<sb1.length() ; i++){

                    char plus = sb1.charAt(i);


                    if(plus == '+'){

                        String[] totaly = finaldata.split("\\+| \\++ | \\+++");

                    if(inc>=1){ 

                        System.out.println(" *******inc value with result is ***************** "+result+"?&&&&&& "+inc);    
                    result = result + Long.parseLong(totaly[inc+1]);

                    }else if(inc<=0){
                        result =  Long.parseLong(totaly[inc]) + Long.parseLong(totaly[inc+1]);
                        //double myDouble = new Long(result).doubleValue();   
                        System.out.println(" Second value is---- ---- "+totaly[inc+1]);
                    }
                     inc = inc +1;
                    }
                    edt.setText("");
                    edt.setText( String.valueOf(result));
                }

            }   

当我把价值加倍时 例如: 12345678 + 32164 比它给我的 答:5.12377842E8 当我试图转换为长于122.81 + 212.122 它给出了零(0)答案 所以请告诉我,我在做什么?正确答案

2 个答案:

答案 0 :(得分:3)

你可以把它们放在表达式中。还有一个问题是你为什么要添加两个long,根据定义它们没有任何小数位,然后将它存储在double中。

long result = 0;


result = (double)result + (double)Long.parseLong(totaly[inc+1]);

阅读本文:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

答案 1 :(得分:1)

我认为你需要这个,尽管你的问题没有明显的证据。

double myDouble = new Long(result).doubleValue();