首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >E的科学表示法

E的科学表示法
EN

Stack Overflow用户
提问于 2015-06-29 03:23:10
回答 3查看 1.1K关注 0票数 0

我试图将数字.0002表示为2.0 x 10 ^ -4。这是我到目前为止所拥有的

代码语言:javascript
运行
复制
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以外的其他任何东西。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-06-29 04:18:48

我将您的方法重新修改为以下内容;您可以使用它作为基础/框架,将双元素转换为您想要的科学表示法,从而完全避免使用E。您可以通过为n > 1n < 0创建实现来扩展它。

代码语言:javascript
运行
复制
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。这是一个替代公式,可以手动发现小数点数,而不是使用字符串方法。

票数 2
EN

Stack Overflow用户

发布于 2015-06-29 03:58:53

您使用的方法会很好,但是有一种更容易使用格式化程序的方法:

代码语言:javascript
运行
复制
    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。

票数 1
EN

Stack Overflow用户

发布于 2015-06-29 04:52:55

这里有一个方法(希望)将所有类型的双值转换为它们的[-]Factor * 10 ^ [-]Exponent表示法。密码里已经解释过了。

编辑:有一个very elegant solutionUnknownOctopus。不过,我还是要把它留在这里,因为它不使用任何形式或类似的,只是双和字符串-我错误地理解了这个问题,并假设只有这样的原语是被允许的。

代码语言:javascript
运行
复制
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));
        }
    }
}

输出:

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31107291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档