我有一个类似于“字符串”的;10.000“;我只想要10作为整数,在java中怎么做?
我有一个像“10.000”这样的字符串,我只想要10作为整数,在java中怎么做 我已经做了`我有一个类似于“字符串”的;10.000“;我只想要10作为整数,在java中怎么做?,java,android,Java,Android,我有一个像“10.000”这样的字符串,我只想要10作为整数,在java中怎么做 我已经做了` public static final Comparator<Product> By_PRICE = new Comparator<Product>() { @Override public int compare(Product o1, Product o2) { //for name // return
public static final Comparator<Product> By_PRICE = new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
//for name
// return o1.getName().compareTo(o2.getName());
// return o1.getFinalPrice().compareTo(o2.getFinalPrice());
return Integer.parseInt(o1.getFinalPrice()) > Integer.parseInt(o1.getFinalPrice()) ? -1
: (Integer.parseInt(o1.getFinalPrice()) < Integer.parseInt(o1.getFinalPrice())) ? 1 : 0;
// if (o1.getFinalPrice().compareTo(o2.getFinalPrice())>0){
// return 1;
// }
// else if (o1.getFinalPrice().compareTo(o2.getFinalPrice())<0){
// return -1;
// }
// else {
// return 0;
// }
}
};
publicstaticfinalcomparatorby_PRICE=newcomparator(){
@凌驾
公共整数比较(产品o1、产品o2){
//名副其实
//返回o1.getName().compareTo(o2.getName());
//返回o1.getFinalPrice().compareTo(o2.getFinalPrice());
返回Integer.parseInt(o1.getFinalPrice())>Integer.parseInt(o1.getFinalPrice())?-1
:(Integer.parseInt(o1.getFinalPrice())0){
//返回1;
// }
//否则如果(o1.getFinalPrice().compareTo)(o2.getFinalPrice())可以使用BigDecimal
String ten = "10.000";
System.out.println(new BigDecimal(ten).intValue());
输出
10
根据使用数学圆()
将字符串拆分为一个点作为分隔符(string.Split(“.”
),然后将其解析为int(Integer.parseInt(string)
),您只需将整个字符串替换为o1.getFinalPrice().compareTo(o2.getFinalPrice())
请删除所有注释行,并提供最小可行代码(),您正试图将MWB的拆分字符串作为数组传递给parseInt。显然,MWB意味着您应该传递数组的第一个元素,即点前面的子字符串:Integer.parse(“10.000.split”()[0])
。因此,您必须先将字符串转换为双精度。它给我的是10.0,而不仅仅是10。请详细说明在我的情况下,它给10.0的位置工作正常。如果您使用双精度和圆形,可能会返回长浮点,则应该工作正常。
System.out.println(Math.round(Float.valueOf("10.000")))