当前位置: 代码迷 >> Java相关 >> String转换为Int或者long
  详细解决方案

String转换为Int或者long

热度:417   发布时间:2009-10-07 16:49:54.0
String转换为Int或者long
怎么将Sring类型的123转换为int或者long类型的123?
搜索更多相关的解决方案: long  String  Int  

----------------解决方案--------------------------------------------------------
long l = Long.parseLong("123");
int i = Integer.parseInt("123");
----------------解决方案--------------------------------------------------------
int i = Integer.parseInt("123");
----------------解决方案--------------------------------------------------------
int i = Integer.parseInt("123");
----------------解决方案--------------------------------------------------------
import javax.swing.JOptionPane;

public class book2_7 {
    public static void main(String []  args){
        String amountString = JOptionPane.showInputDialog(
                "Enter an amount in double,for example 123.15");
        double amount = Double.parseDouble(amountString);
        int remainingAmount=(int)(amount*100);
        int numberOfOneDollars = remainingAmount/100;
        remainingAmount=remainingAmount%100;
         
        int numberOfQuarters=remainingAmount/25;
        remainingAmount=remainingAmount%25;
         
        int numberOfDimes= remainingAmount/10;
        remainingAmount=remainingAmount%10;
         
        int numberOfNickels=remainingAmount/5;
        remainingAmount=remainingAmount%5;
         
        int numberOfPannies=remainingAmount;
         
        String output="Your amount "+amount+" consists of \n"
        +numberOfOneDollars+" dollars\n"+numberOfQuarters+
        " quarters\n"+numberOfDimes+" dimes\n"+numberOfNickels+
        " nickels\n"+numberOfPannies+" pannies\n";
        JOptionPane.showMessageDialog(null,output);
    }
     
}
----------------解决方案--------------------------------------------------------
  相关解决方案