[英]Scientific notation with E
我试图将数字.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
以外的任何东西。
我将您的方法重做如下: 您可以将其用作基础/骨架,以将双精度转换为所需的科学计数法,从而完全避免使用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
乘以counter
次数10,直到n
大于1。这是一个替代公式,用于手动发现小数点数而不是使用String方法。
您使用的方法可以正常工作,但是使用格式化程序有一种更简单的方法:
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。
这是一种(希望)将所有类型的double转换为它们的[-]Factor * 10 ^ [-]Exponent
表示法的方法。 在代码内部进行了解释。
编辑: 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 ^ 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.