Java数值转换代码 java数据转换
Java中几种常用数据类型之间转换的方法
下面给你例举10种Java中几种常用的数据类型之间转换方法:
创新互联专注于企业营销型网站建设、网站重做改版、商河网站定制设计、自适应品牌网站建设、H5响应式网站、成都商城网站开发、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为商河等各大城市提供网站开发制作服务。
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();
}
});
btnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txtIn.setText("0");
txtResult.setText("0");
}
});
rdLeng.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cboIn.setModel(new DefaultComboBoxModel(lengthUnit));
cboOut.setModel(new DefaultComboBoxModel(lengthUnit));
}
});
rdWeig.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cboIn.setModel(new DefaultComboBoxModel(timeUnit));
cboOut.setModel(new DefaultComboBoxModel(timeUnit));
}
});
rdLeng.setSelected(true);
cboIn.setModel(new DefaultComboBoxModel(lengthUnit));
cboOut.setModel(new DefaultComboBoxModel(timeUnit));
}
final int offX=100;
public void doLayout(){
super.doLayout();
rdLeng.setBounds(offX, 15, 60, 20);
rdWeig.setBounds(rdLeng.getX()+rdLeng.getWidth()+5, 15, 60, 20);
lblInUnit.setBounds(offX, rdLeng.getY()+rdLeng.getHeight()+20, 80, 20);
cboIn.setBounds(lblInUnit.getX()+lblInUnit.getWidth()+5, lblInUnit.getY(), 80, 20);
lblIn.setBounds(offX, lblInUnit.getY()+lblInUnit.getHeight()+5, 80, 20);
txtIn.setBounds(lblIn.getX()+lblIn.getWidth()+5, lblIn.getY(), 80, 20);
lblOutUnit.setBounds(offX, lblIn.getY()+lblIn.getHeight()+30, 80, 20);
cboOut.setBounds(lblOutUnit.getX()+lblOutUnit.getWidth()+5, lblOutUnit.getY(), 80, 20);
lblResult.setBounds(offX, cboOut.getY()+cboOut.getHeight()+5, 80, 20);
txtResult.setBounds(lblResult.getX()+lblResult.getWidth()+5, lblResult.getY(), 100, 20);
int w=getWidth ();
int x=(w-70*2-5)/2;//水平居中
btnTrans.setBounds(x, lblResult.getY()+lblResult.getHeight()+30, 70, 25);
btnClear.setBounds(btnTrans.getX()+btnTrans.getWidth()+3, btnTrans.getY(), 70, 25);
}
public void doConvert(){
double v=0;
try{
v= Double.parseDouble(txtIn.getText());
}catch(Exception ex){
txtIn.setText("0");
return;
}
//"米", "分米", "厘米", "毫米"
if(rdLeng.isSelected()){
switch(cboIn.getSelectedIndex()){
case 0:
break;
case 1:
v=v/10;
break;
case 2:
v=v/100;
break;
case 3:
v=v/1000;
break;
default:
return;
}
//v 现在是标准单位:米
switch(cboOut.getSelectedIndex()){
case 0:
break;
case 1:
v=v*10;
break;
case 2:
v=v*100;
break;
case 3:
v=v*1000;
break;
default:
return;
}
if(v0.01){
txtResult.setText(String.format("%2.8f", v));
}else{
txtResult.setText(String.format("%2.2f", v));
}
}else{
//"天", "时", "分", "秒"
switch(cboIn.getSelectedIndex()){
case 0:
v=v*24;
break;
case 1:
break;
case 2:
v=v/60;
break;
case 3:
v=v/3600;
break;
default:
return;
}
//v 现在是标准单位:小时
switch(cboOut.getSelectedIndex()){
case 0:
v=v/24;
break;
case 1:
break;
case 2:
v=v*60;
break;
case 3:
v=v*3600;
break;
default:
return;
}
if(v0.01){
txtResult.setText(String.format("%2.8f", v));
}else{
txtResult.setText(String.format("%2.8f", v));
}
}
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
public void run() {
new UnitTransfer();
}
});
}
}
//请参考,欢迎指正
JAVA中怎么将int数据转换为byte数据?
JAVA中根据以下代码将int数据转换为byte数据:
public static byte[] int32ToBytes(int val) {
int size = Integer.SIZE / Byte.SIZE;
byte[] ret = new byte[size];
for (int i = 0; i size; ++i) {
ret[i] = (byte) (val (8 * i) 56);
}
return ret;
}
扩展资料
Java也提供了一个byte数据类型,并且是基本类型。java byte是做为最小的数字来处理的,因此它的值域被定义为-128~127,也就是signed byte。
byte,即字节,由8位的二进制组成。在Java中,byte类型的数据是8位带符号的二进制数。在计算机中,8位带符号二进制数的取值范围是[-128, 127],所以在Java中,byte类型的取值范围也是[-128, 127]。
运算规则:
####正数的最高位都是 0 ,正数的值就是二进制表示的值。 ####
####负数的最高位都是 1 ,负数的值是 取反后加一 然后加个负号得到得值。 ####
新闻标题:Java数值转换代码 java数据转换
本文路径:http://scjbc.cn/article/docggcd.html