我试图将数字.0002表示为2.0 x 10 ^ -4。这是我到目前为止所拥有的
public static String toScientificNotation(double n) {
int exponent = 0;
if( n < 1){
String doubleValue = Double.toString(Math.abs(n));
int format = doubleValue.indexOf(".");
int decimalPlacesToMove = (doubleValue.length() - (format - 1));
}无论我如何尝试,我都会在输出中获得E。如果有人能给我一个伪代码。这会是很大的帮助。我不能使用BigDecimal或除double以外的其他任何东西。
发布于 2015-06-29 04:18:48
我将您的方法重新修改为以下内容;您可以使用它作为基础/框架,将双元素转换为您想要的科学表示法,从而完全避免使用E。您可以通过为n > 1和n < 0创建实现来扩展它。
private static String toScienticNotation(double n) {
String result = "";
if (n < 1 && n > 0) {
int counter = 0;
double answer = n;
while (answer < 1) {
answer = answer * 10;
counter--;
}
result = String.valueOf(answer) + " x 10 ^ "
+ String.valueOf(counter);
}
return result;
}它的工作方式是将输入n乘以10,counter次数,直到n大于1。这是一个替代公式,可以手动发现小数点数,而不是使用字符串方法。
发布于 2015-06-29 03:58:53
您使用的方法会很好,但是有一种更容易使用格式化程序的方法:
import java.util.*;
import java.text.*;
import java.math.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
NumberFormat formatter = new DecimalFormat();
double d = input.nextDouble();
formatter = new DecimalFormat("#.######E0");
String x = formatter.format(d);
System.out.println(x.replace("E","*10^");
}
}这将以#.######E0的十进制格式打印科学符号。
例如:
如果输入200,系统将返回2* 10^2。
发布于 2015-06-29 04:52:55
这里有一个方法(希望)将所有类型的双值转换为它们的[-]Factor * 10 ^ [-]Exponent表示法。密码里已经解释过了。
编辑:有一个very elegant solution由UnknownOctopus。不过,我还是要把它留在这里,因为它不使用任何形式或类似的,只是双和字符串-我错误地理解了这个问题,并假设只有这样的原语是被允许的。
public class Main{
/**
* Converts a double to a base10 notation String.
*
* Each String is formatted like this:
*
* [-]Factor * 10 ^ [-]Exponent
*
* where both, Factor and Exponent, are integer values.
*
* @param number the number to convert
* @return a base10 notation String.
*/
public static String toScientificNotation(double number) {
String s = String.valueOf(number);
int indexPZero = s.indexOf(".0"); // mostly to check if .0 is the end
int exponent = 0; // simplest case: *10^0
// Check if the String ends with exactly .0
if (indexPZero == s.length() - 2) {
// If the string also has 0s in front of the period, shift those to the
// right
while(s.contains("0.")) {
number /= 10;
exponent += 1;
s = String.valueOf(number);
}
// if the string ends in .0 and has no zeros in front of the period we
// can format it:
return String.valueOf(number) + " * 10 ^ " + exponent;
}
// If the String does not end in .0, we need to shift to the left.
// Additionall
while (indexPZero != s.length() -2) {
// in case we suddenly reach the scientific notation just substitute it
s = s.toLowerCase();
if (s.contains("e")) {
return s.substring(0,
s.indexOf("e")) + " * 10 ^ " + s.substring(s.indexOf("e")+1);
}
// otherwise shift left and reduce the exponent
number *= 10;
exponent -= 1;
s = String.valueOf(number);
indexPZero = s.indexOf(".0");
}
// If we end up here, just write out the number and the exponent.
return String.valueOf(number) + " * 10 ^ " + exponent;
}
public static void main(String... args) {
double[] vals = { 1, 0.2, 23.4, -32.00004, 0.0002, 10.0 };
for(double val : vals) {
System.out.println(val + " becomes " + toScientificNotation(val));
}
}
}输出:
1.0 becomes 1.0 * 10 ^ 0
0.2 becomes 2.0 * 10 ^ -1
23.4 becomes 234.0 * 10 ^ -1
-32.00004 becomes -3200004.0 * 10 ^ -5
2.0E-4 becomes 2.0 * 10 ^ -4
10.0 becomes 1.0 * 10 ^ 1https://stackoverflow.com/questions/31107291
复制相似问题