我想让我的号码移动第一个字符到最后。
例如,我会得到我的双d。
double d = 12345.6;
double result = 2345.61;
目前我正在删除d的第一个字符:
d = d % (int) Math.pow(10, (int) Math.log10(d));
但我不知道如何存储我要移除的角色,所以我可以把它放在最后。我知道我可以将d转换为数组或字符串,但我希望尽可能将它保持为double。
我使用Instant.now从纳秒时钟获得双倍,所以我可以保证它以8位正整数开始,我首先添加.0以便我可以使它成为双倍。我知道我可以使用字符串(正如我在帖子中提到的那样),但我想知道是否有办法在没有转换的情况下进行。
谢谢你的帮助!
(这是我的第一篇文章,如果不好,我道歉)
这是一个非常棘手的问题,这不是一个有效的答案,但它非常接近。我遇到的唯一麻烦是java在我的双打结尾添加小数,因为它不能很好地代表一个数字。我的解决方案可能会让您走上正确的道路。 appendChar
搞砸了,因为它在双尾的末尾添加了数字,除了这个问题可能会有效。
public static void main(String[] args) {
double test = 4234.1211;
boolean hasDecimals = test % 1 > 0;
double[] leadChar = getLeadDigitAndMulti(test);
double appendedValue = appendLeadChar(test, (int) leadChar[1], hasDecimals);
}
public static double[] getLeadDigitAndMulti(double value) {
double[] result = new double[2];
int multiplier = 10;
int currentMultiplier = 1;
while (value > currentMultiplier) {
currentMultiplier *= multiplier;
}
currentMultiplier /= multiplier;
double trail = value % (currentMultiplier);
result[0] = currentMultiplier;
double lead = value - trail;
lead /= currentMultiplier;
result[1] = lead;
return result;
}
public static double appendLeadChar(double value, int leadChar, boolean hasDecimals) {
if (!hasDecimals) {
return (value * 10) + leadChar;
}
int multiplier = 10;
double currentMultiplier = 1;
while (value > 0) {
value %= currentMultiplier;
currentMultiplier = currentMultiplier / multiplier;
}
return 0;
}
我首先要弄清楚你的double
有多少位数。我不太确定如何使它工作,但我看到了另一个可以回答它的问题。这是该主题的链接:Number of decimal digits in a double
而我能找到的最接近的代码就是:
double d= 234.12413;
String text = Double.toString(Math.abs(d));
int integerPlaces = text.indexOf('.');
int decimalPlaces = text.length() - integerPlaces - 1;
注意这段代码不是矿山,归功于Peter Lawrey,他最初在链接的帖子中回答了这个问题。
然后使用类似于上面的代码或修改代码^假设您将找到如何查找double的长度或double的总位数,您将创建一个新的int或double变量,它将存储double并使用它来舍入它BigDecimal到最高位值。将使用10*pow(x)
设置BigDecimal将舍入到的位置,其中x是小数点后的位数。
抱歉,我无法提供有关如何实际运行的代码,因为我对java很新。这是我在更详细的伪代码中的意思:
double d = 12345.6; //the double you are evaluating
double dNoDecimal; //will be rounded to have no decimal places in order to calculate place of the 1st digit
double dRoundedFirstDigit; //stores the rounded number with only the 1st digit
double firstDigit; //stores first digit
dNoDecimal = d; //transfers value
dNoDecimal = [use BigDecimal to round so no decimal places will be left]
//dNoDecimal would be equal to 12345 right now
[use above code to find total amount of place values]
[knowing place values use BigDecimal to round and isolate 1st digit of double]
dRoundedFirstDigit = dNoDecimal; //transfers value
dRoundedFirstDigit = [round using BigDecimal to leave only 1st digit]
firstDigit = [dRoundedFirstDigit cut down to 1st digit value using length found from before, again an equation will be needed]
//Now use reverse process
//do the same thing as above, but find the number of decimals by rounding down to leave only decimals (leaving only .6)
//find the length
//use length to calculate what to add to the original value to move the digit to the end (in this case would be d += firstDigit*Math.pow(10,-2))
如果我让一切看起来更令人困惑,我道歉,但我尽力了,希望这会有所帮助。
此外,没有数组或字符串这样做真的很难......我可以问你为什么要在没有数组或字符串的情况下这样做?