如何在不转换为字符串的情况下将双倍中的第一个数字移到末尾?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (68)

我想让我的号码将第一个字符移动到最后,会得到我的双d

double d = 12345.6;  
double result = 2345.61;

目前我正在删除d的第一个字符:

d = d % (int) Math.pow(10, (int) Math.log10(d));
提问于
用户回答回答于

能找到的最接近的代码就是:

double d= 234.12413;
String text = Double.toString(Math.abs(d));
int integerPlaces = text.indexOf('.');
int decimalPlaces = text.length() - integerPlaces - 1;

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)) 

扫码关注云+社区

领取腾讯云代金券