Java数值转换代码 java数值转换成字符串

java中 将数字转换成字符方法一:直接强制转换 。如:Stringstr= (String)123;
方法二:直接通过空字符串+数字的形式转换为字符串(前后都可以用) 。如:String str= ""+123;
方法三:直接通过包装类来实现 。如:Stringstr = String.valueOf(1231);
Java中几种常用数据类型之间转换的方法下面给你例举10种Java中几种常用的数据类型之间转换方法:
1. short--int 转换
exp: short shortvar=0;
int intvar=0;
shortvar= (short) intvar
2. int--short 转换
exp: short shortvar=0;
int intvar=0;
intvar=shortvar;
3. int-String 转换
exp: int intvar=1;
String stringvar;
Stringvar=string.valueOf (intvar);
4. float-String 转换
exp: float floatvar=9.99f;
String stringvar;
Stringvar=String.valueOf (floatvar);
5. double-String 转换
exp double doublevar=99999999.99;
String stringvar;
Stringvar=String.valueOf (doublevar);
6. char-String 转换
exp char charvar=’a’;
String stringvar;
Stringvar=String.valueOf (charvar);
7 String-int、float、long、double 转换
Exp String intstring=”10”;
String floatstring=”10.1f”;
String longstring=”99999999”;
String doubleString=”99999999.9”;
Int I=Integer.parseInt (intstring);
Float f= Integer.parseInt (floatstring);
Long lo=long. parseInt (longstring);
Double d=double. parseInt (doublestring);
8 String-byte、short 转换
Exp String s=”0”;
Byte b=Integer.intValue(s);
Short sh=Integer.intValue(s);
9 String-char 转换
Exp String s=”abc”;
Char a=s.charAt(0);
返回字符a
10 String--boolean 转换
String s=”true”;
Boolean flag=Boolean.valueOf (“s”);
数据单位转换工具java代码import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class UnitTransfer extends JFrame{
private final int WIDTH = 400, HEIGHT = 300;//窗口默认的宽度、高度
private JLabel lblInUnit=new JLabel("输入单位");
private JComboBox cboIn=new JComboBox(new String[]{"", ""});
private JLabel lblIn=new JLabel("输入数值");
private JTextField txtIn=new JTextField("10");
private JLabel lblOutUnit=new JLabel("输出单位");
private JLabel lblResult=new JLabel("显示结果");
private JLabel txtResult=new JLabel("结果");
private JComboBox cboOut=new JComboBox(new String[]{"", ""});
private JButton btnTrans = new JButton("转换");
private JButton btnClear = new JButton("清空");
private JRadioButton rdLeng = new JRadioButton("长度");
private JRadioButton rdWeig = new JRadioButton("时间");
private String [] lengthUnit={"米", "分米", "厘米", "毫米"};
private String [] timeUnit={"天", "时", "分", "秒"};
public UnitTransfer(){
super("简单的单位转换器 Beta");
this.setSize(WIDTH, HEIGHT);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonGroup group = new ButtonGroup();
group.add(rdLeng);
group.add(rdWeig);
this.getContentPane().add(rdLeng);
this.getContentPane().add(rdWeig);
this.getContentPane().add(btnTrans);
this.getContentPane().add(btnClear);
this.getContentPane().add(lblIn);
this.getContentPane().add(txtIn);
this.getContentPane().add(lblInUnit);
this.getContentPane().add(cboIn);
this.getContentPane().add(lblResult);
this.getContentPane().add(txtResult);
this.getContentPane().add(lblOutUnit);
this.getContentPane().add(cboOut);
this.setVisible(true);
this.doLayout();
btnTrans.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
doConvert();

推荐阅读