Convert a number from scientific notation to decimal in JAVA

馋奶兔 提交于 2019-12-10 19:08:16

问题


I have a problem: a number is showing in scientific notation if it has 8 or more digits before the decimal point.
Is there a simple way to convert this number to decimal via a library or something?
I began creating a manual method to parse it out, but it seems overcomplicated.

Any help will be appreciated.

input example: 1.0225556677556E7

Edit: I also need to be able to identify a number that is in scientific notation


回答1:


You can use NumberFormat your accomplish your goal easily.

String scientificNotation = "1.0225556677556E7";
Double scientificDouble = Double.parseDouble(scientificNotation);
NumberFormat nf = new DecimalFormat("################################################.###########################################");
decimalString = nf.format(scientificDouble);

To answer you're other question about matching scientific notation strings- you can use a regex and String.matches(). This one isn't perfect although the probability of getting false positives should be very low:

if(myString.matches("-?[\\d.]+(?:E-?\\d+)?")){
    //do work
}


来源:https://stackoverflow.com/questions/30646582/convert-a-number-from-scientific-notation-to-decimal-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!