我需要将float转换为int,就好像删除了逗号一样.示例:23.2343f - > 232343
private static int removeComma(float value)
{
for (int i = 0; ; i++) {
if((value * (float)Math.pow(10, i)) % 1.0f == 0.0f)
return (int)(value * Math.pow(10, i));
}
}
Run Code Online (Sandbox Code Playgroud)
问题在于数字的四舍五入.例如,如果我传递23000.2359f它变为23000236,因为它将输入四舍五入到23000.236.
Java float没有那么多精度,你可以看到
float f = 23000.2359f;
System.out.println(f);
Run Code Online (Sandbox Code Playgroud)
哪个输出
23000.236
Run Code Online (Sandbox Code Playgroud)
要获得所需的输出,您可以使用double类似的
double d = 23000.2359;
String v = String.valueOf(d).replace(".", "");
int val = Integer.parseInt(v);
System.out.println(val);
Run Code Online (Sandbox Code Playgroud)
输出是(要求的)
230002359
Run Code Online (Sandbox Code Playgroud)