
正文
单位换算java代码 java currenttimemillis单位
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java中把米换算成公里
private int dist = 789;
private double dis = 0;
//你的距离数据应该不是写死的吧,如果你是从服务器获取的距离数据,可能是String,赋值给//distance时候就要强制类型转换(Integer),然后再执行以下四舍五入
dis = Math.round(dist/100d)/10d;
disText.setText(dis+"公里")
//System.out.println("距离:"+disText);
/**写的可能不规范,但就是这个意思,应该是你正在做的东西**/
相关问答
Q1: 数据单位转换工具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();
}
});
}
}
//请参考,欢迎指正
Q2: java swing单位换算问题,加急~~~~~~~
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) {
// System.out.println(toProperUnit("1thz","hz"));
// System.out.println(toProperUnit("1mhz","khz"));
// System.out.println(toProperUnit("2.01ghz","khz"));
// System.out.println(toProperUnit("1003hz","khz"));
/*output:
1000000000000Hz
1000KHz
2010000KHz
1.003KHz
* */
// printAllSub("0.9ghz");
// printAllPre("900000hz");
/*output:
0.9ghz - MHz: 900MHz
0.9ghz - KHz: 900000KHz
0.9ghz - Hz: 900000000Hz
900000hz - KHz: 900KHz
900000hz - MHz: 0.9MHz
900000hz - GHz: 0.0009GHz
900000hz - THz: 0.0000009THz
*/
System.out.println(autoUnit("0.9mhz"));
System.out.println(autoUnit("0.09mhz"));
System.out.println(autoUnit("0.009mhz"));
System.out.println(autoUnit("0.0009mhz"));
System.out.println(autoUnit("0.00009mhz"));
System.out.println(autoUnit("0.000009mhz"));
/*
output:
900KHz
90KHz
9KHz
900Hz
90Hz
9Hz
*/
}
private static final DecimalFormat nf = new DecimalFormat();
private static final String[] units = {"THz","GHz","MHz","KHz","Hz",};
private static final BigDecimal kk = new BigDecimal("1000");
private static final BigDecimal ks = new BigDecimal("0.001");
private static final String allChars = "[^tgmkhzTGMKHZ]";
static{
nf.setMaximumFractionDigits(units.length*3);
nf.setGroupingUsed(false);
}
// public static void printAllSub(String in){
// String u = in.replaceAll(allChars,"");
// int index = findUnitIndex(u);
// if(index0){
// System.out.println("Can not found proper unit in units array."+in);
// return;
// }
// for(int i=index+1; iunits.length; i++){
// System.out.printf("%s - %s: %s%n",in,units[i],toProperUnit(in,units[i]));
// }
// }
// public static void printAllPre(String in){
// String u = in.replaceAll(allChars,"");
// int index = findUnitIndex(u);
// if(index0){
// System.out.println("Can not found proper unit in units array."+in);
// return;
// }
// for(int i=0; iindex; i++){
// System.out.printf("%s - %s: %s%n",in,units[index-i-1],toProperUnit(in,units[index-i-1]));
// }
// }
public static String autoUnit(String in){
String u = in.replaceAll(allChars,"");
int index = findUnitIndex(u);
if(index0)
return in;
BigDecimal bd=null;
try{
bd = new BigDecimal(in.replaceAll("[^\\.0-9]",""));
while(bd.remainder(BigDecimal.ONE).compareTo(BigDecimal.ZERO)0 indexunits.length-1){
bd=bd.multiply(kk);
++index;
}
return nf.format(bd)+units[index];
}catch(Exception e){}
return in;
}
// public static String toProperUnit(String in,String unit){
// String u = in.replaceAll(allChars,"");
// int index = findUnitIndex(u);
// int dest = findUnitIndex(unit);
// if(index0){
// return in;
// }
//
// BigDecimal bd=null;
// try{
// bd = new BigDecimal(in.replaceAll("[^\\.0-9]",""));
// }catch(Exception e){}
// if(bd==null)
// return in;
// boolean less = indexdest;
// for(int i=0; iMath.abs(index-dest); i++){
// bd=bd.multiply(less?kk:ks);
// }
//
// return nf.format(bd)+units[dest];
// }
private static int findUnitIndex(String u) {
for(int i=0; iunits.length; i++)
if(units[i].equalsIgnoreCase(u))
return i;
return -1;
}
}
单位换算java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java currenttimemillis单位、单位换算java代码的信息别忘了在本站进行查找喔。






