java与js计算的使用
js计算的使用js保留小数位数
function hj(){
var hsldl = 0;
$.each($(":input[name='sld']"),function(){ //input属性选择器
hsldl += parseFloat(this.value);
});
$("#hjjcl").val(hsldl.toFixed(4)); //保留四位小数
}
js的加法
var hj =parseFloat($(":input[name='yl']").eq(0).val())+parseFloat(list[0].ymkIbean.data['SUM(JH)']);
JavaScript 四舍五入的方法:
小数点问题
Math.round(totalAmount*100)/100 (保留 2 位)
function formatFloat(src, pos)
{
return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}
模拟计算器,计算总价钱
window.onload=function () {
var price=document.myform.price;
var nums=document.myform.nums;
var con=document.myform.con;
nums.onchange=function () {
var aa=nums.selectedIndex+1;
con.value=aa*parseInt(price.value);
}
}
价钱:
数量:
1个
2个
3个
总价钱:
java计算的使用
字符串转化为整形、浮点类型
String s = "100";
//方法一
int a = Integer.parseInt(String s);
Long.parseLong(String s);
Float.parseFloat(String s);
Double.parseDouble(String s)
//方法二
int a = Integer.valueOf(s).intValue();
不同之处:Integer.parseInt(String s)生成的是一个整形; Integer.valueOf(s).intValue()生成的是一个对象;
整形、浮点类型转化为字符串
int i=11;
//方法一
String s=i+"";
//方法二
String s=String.valueOf(i);
//方法三
String s=Integer.toString(i);
Map map = new HashMap();
InstallCompleteVo installCompleteVo = installCompleteVoMapper.getUid(installAcceptId);
Double companyHj = Double.parseDouble(installCompleteVo.getCompanyDeviceMoney())+Double.parseDouble(installCompleteVo.getCompanyInstallMoney());
Double userMoneyHj = Double.parseDouble(installCompleteVo.getUserDeivceMoney())+Double.parseDouble(installCompleteVo.getUserInstallMoney());
installCompleteVo.setCompanyHj(companyHj.toString());
installCompleteVo.setUserMoneyHj(userMoneyHj.toString());
map.put("installApplyCheckVo", installCompleteVo);
function hj(){
var hsldl = 0;
$.each($(":input[name='sld']"),function(){ //input属性选择器
hsldl += parseFloat(this.value);
});
$("#hjjcl").val(hsldl.toFixed(4)); //保留四位小数
}
Java保留小数位数
sum = String.valueOf(sum1);
dataList.add(new java.text.DecimalFormat("#.0000").format(sum));
return dataList;
保留两位小数{
方法一:{
double c=3.154215;
java.text.DecimalFormat myformat=new java.text.DecimalFormat("0.00");
String str = myformat.format(c);
}
方式二:{
java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
df.format(你要格式化的数字);
例:new java.text.DecimalFormat("#.00").format(3.1415926)
#.00 表示两位小数 #.0000四位小数 以此类推...
}
方式三:{
double d = 3.1415926;
String result = String .format("%.2f");
%.2f %. 表示 小数点前任意位数 2 表示两位小数 格式后的结果为f 表示浮点型
}
}
四舍五入 {
double f = 111231.5585;
BigDecimal b = new BigDecimal(f);
//保留2位小数
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
java四舍五入
double f = 4.025;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(f1);
String strF3 = String.format("%.2f", 4.025);
System.out.println(strF3)
BigDecimal进行加法减运算
/**
* 提供精确的加法运算。
* @param v1 被加数
* @param v2 加数
* @return 两个参数的和
*/
public static double add(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
BigDecimal进行减法运算
String selSql = "select TJZ from DATE_TJ where BH ='"+mc+"'and SJ=?";
IBean ibeans = SqlUtil.queryForBean(selSql, qn);
String yql = ibean.get("TJZ", ""));
String qnyql = "";
if(ibeans==null){
tqYql.add("0");
qnyql = "0";
}
/*String tjce = String.valueOf(Float.parseFloat(yql) - Float.parseFloat(qnyql));*/
BigDecimal b1 = new BigDecimal(Float.toString(Float.parseFloat(TJZ)));
BigDecimal b2 = new BigDecimal(Float.toString(Float.parseFloat(TJZl)));
float ss = b1.subtract(b2).floatValue();
float tjce = Float.parseFloat(String.valueOf(ss));
ce.add(String.valueOf(tjce));
BigDecimal进行乘除运算
public Map showPipeList(PipeVO vo)
{
Map maps = new HashMap();
List yearCovlist = new ArrayList();
List orglist = new ArrayList();
List rushCount = pipeMapper.showPipeList(vo);
for (PipeVO pipeVO : rushCount) {
BigDecimal yearCov = new BigDecimal("0");
BigDecimal hundred = new BigDecimal("100");
String counts = pipeVO.getRunUser();
String orgs = pipeVO.getDepOrder();
String dayCov = pipeVO.getDayCovRate();
if(null!=counts&&null!=dayCov) {
BigDecimal num1 = new BigDecimal(counts);
BigDecimal num2 = new BigDecimal(dayCov);
BigDecimal yearCovs = num2.divide(num1,2, BigDecimal.ROUND_HALF_UP);
yearCov = yearCovs.multiply(hundred);
}
String eventCauseName = compareDic(orgs);
yearCovlist.add(eventCauseName);
orglist.add(yearCov.toString());
}
maps.put("yearCovlist", yearCovlist);
maps.put("orglist", orglist);
return maps;
}