我正在尝试删除代码通过数字512运行时的小数点,但将其他数字保留在小数点后。我已经尝试过使用if语句和字符串格式化方法,但不幸的是到目前为止,什么都没有起作用。
public class Main
{
public static void main(String[] args)
{
System.out.println(Challenge.formatAsMemory(512));
System.out.println(Challenge.formatAsMemory(1536));
}
}
public class Challenge
{
public static String formatAsMemory(long bytes)
{
double newNum = bytes;
if(newNum < 1048576){
newNum = newNum/1024;
}else if(newNum < 3048576){
newNum = newNum/1048576;
}else if(newNum < 1632087573){
newNum = newNum / 1073741824;
}else if (newNum < 13421772801L){
newNum = newNum / 1073741824;
}else if(newNum < 23412048474L){
newNum = newNum / 1073741824;
}else if(newNum < 1231453023110L){
newNum = newNum / 1073741824;
newNum = newNum / 1024;
}
String numberAsString = String.format ("%.2f", newNum);
double number = Double.parseDouble(numberAsString);
if (number == 0.50){
number = 512;
}
String answer = Double.toString(number);
return answer;
}
答案 0 :(得分:-2)
这将根据您的描述起作用
if (number == 0.50){
return "512";
}