public static decimal GetSingleTON(string specification) { //sample:2.2KG*5/箱 decimal dm = 1; if (!string.IsNullOrEmpty(specification)) { //step 1 将specification转小写后,按[*]分割 specification = specification.ToLower(); string[] strs = specification.Split('*'); List<decimal> parms = new List<decimal>(); foreach (string str in strs) { if (str.Contains(".")) { string result = Regex.Replace(str, @"[^\d.\d]", ""); //小数 decimal val = decimal.Parse(result); dm = dm * val; } else { string result = System.Text.RegularExpressions.Regex.Replace(str, @"[^0-9]+", ""); //整数 decimal val = decimal.Parse(result); dm = dm * val; } } if (specification.Contains("kg") || specification.Contains("千克") || specification.Contains("公斤")) { dm = dm / 1000; } else if (specification.Contains("g") || specification.Contains("克")) { dm = dm / 1000000; } } return dm; }转换成java代码
时间: 2023-06-04 11:08:54 浏览: 120
公共静态十进制数 GetSingleTON(String specification){
十进制数 dm = new 十进制数(1);
如果(!字符串.isNullOrEmpty(specification)){
specification = specification.toLowerCase();
字符串[] strs = specification.split("*");
List< 十进制数 > parms = new ArrayList< 十进制数 >();
for(字符串 str : strs){
如果(str.contains(".")){
字符串 result = str.replaceAll("[^\\d.\\d]", "");
十进制数 val = new 十进制数(result);
dm = dm.multiply(val);
}else{
字符串 result = str.replaceAll("[^0-9]", "");
十进制数 val = new 十进制数(result);
dm = dm.multiply(val);
}
}
如果(specification.contains("kg") || specification.contains("千克") || specification.contains("公斤")){
dm = dm.divide(new 十进制数(1000));
}else 如果(specification.contains("g") || specification.contains("克")){
dm = dm.divide(new 十进制数(1000000));
}
}
返回 dm;
}
阅读全文