0

so i have a xml string that looks like this:

<CONFIG><Setting1><o1>44</o1><o2>1.0E-4</o2><o3>955</o3><o4>1.5E-4</o4><o5>Surname</o5></setting1>....</CONFIG>

How would i go about converting every float in a string from scientific-notion to the decimal-notation?

Edit: To clarify, im not looking to convert only a single float value from scientific to decimal nation. The String is read from a xml file that i serialized from a pojo, so all of the float values in the String would need to be converted. Sadly the XML-Framework i used (SimpleXML) only represents floats in scientific notation.

UPDATE: Tried finding the float values with RegEx, it works. "found" will be the new converted decimal. How would i go about replacing each of the the found pattern with the "found"-String?

    public static void ScientificToDecimal(String text){
    String found;
    Pattern pattern = Pattern.compile("\\d+[.]\\d+E[+-]\\d");
    Matcher matcher = pattern.matcher(text);
    while(matcher.find()){
        found = new BigDecimal(matcher.group()).toPlainString();
        Log.i("Converted: ", matcher.group() + " to " + found);
        }
    }

UPDATE2: Works good enough for me.

    public static String scientificToDecimal(String text){
    String replacementText = "";
    StringBuffer sb = new StringBuffer();

    Pattern pattern = Pattern.compile("\\d+[.]\\d+E[+-]\\d");
    Matcher matcher = pattern.matcher(text);

    while(matcher.find()){

        replacementText = new BigDecimal(matcher.group()).toPlainString();
        matcher.appendReplacement(sb,replacementText);
        Log.i("Converted: ", matcher.group() + " to " + replacementText);
    }
    matcher.appendTail(sb);

    return sb.toString();
    }
p_d
  • 101
  • 1
  • 12
  • Did you try anything? – Oliver Charlesworth Jul 10 '17 at 21:50
  • 1
    Possible duplicate of [Convert a number from scientific notation to decimal in JAVA](https://stackoverflow.com/questions/30646582/convert-a-number-from-scientific-notation-to-decimal-in-java) – yasin Jul 10 '17 at 22:06
  • You could use [`BigDecimal`](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html). e.g. . `String result = new BigDecimal("1.0E-4").toPlainString();` – d.j.brown Jul 10 '17 at 22:08
  • @OliverCharlesworth Well i know that i can convert a single value with BigDecimal, but i would need to parse the whole String and convert every single float to the decimal notation. The initial XML String is also way longer and there's probably ~100 floats in E-notation that would need to be converted to decimal notation. Not really sure how to approach the parsing tho. – p_d Jul 10 '17 at 22:13
  • 1
    You really need XPath to extract the text values reliably, and `Double.parseDouble()` or `BigDecimal` to do the conversion. – user207421 Jul 10 '17 at 23:40
  • You have asked your question twice. You are getting more traction here, so the other I have voted to close. https://stackoverflow.com/q/45020709/1224741 – QED Jul 12 '17 at 16:38
  • What did you wind up doing? – QED Jul 19 '17 at 14:27
  • updated my post with the solution i ended up using. – p_d Jul 23 '17 at 00:02

3 Answers3

0

Think about those pattern >1.0E-4< or >1.5E-4< and RegEx and String replacement and so on.

Quang Nguyen
  • 354
  • 1
  • 5
  • 20
  • Hey, thanks. I'm still pretty new to java, just read about regex. Would this be fitting \d+.\d+E[-+]\d? Also, to find the pattern, can i use pattern/matcher? – p_d Jul 10 '17 at 23:21
0

Use XMLPullParser (consult the guide) to get the double values, then convert using the technique described here, or here, potentially use your regex.

QED
  • 9,803
  • 7
  • 50
  • 87
  • Is it not possible to simply replace the current selected pattern from matcher.find() with the converted value? It works like a charm and i'm getting back all the values that i actually need to convert. I'll have a look at the xmlpullparser anyway tho, thanks. – p_d Jul 11 '17 at 15:41
  • Admittedly, I don't know much about SimpleXML or what you are doing with it. But if *I* were pulling values from XML, I would not use a regular expression engine. https://stackoverflow.com/a/1732454/1224741 – QED Jul 12 '17 at 16:31
  • Well the SimpleXML part is done and works. I just have a XML-String now that i treat like a normal String. Basically finding all the float values with the RegEx works, i can also extract and convert them from scientific to decimal notation. The part i'm struggling with is replacing the found float values in scientific notation with the converted float values. And since they're all different, i can't do String.replaceAll(). I looked into XMLPullparser, but that also can't replace the values one after the other. – p_d Jul 12 '17 at 21:45
  • You should parse the XML and re-emit with values converted per the linked. Abandon the RegEx nonsense, it is the wrong tool for the job. – QED Jul 13 '17 at 02:47
0

Just to enhance to handle the following scenarios

a) 1.0E-4 b) 1.0E4 c) 1.0E+4

public static String scientificToDecimal(String text){

    String out = "";
    boolean found = false;
    String replacementText = "";
    StringBuffer sb = new StringBuffer();

    /*
     * 5.0E4
     */
    Pattern pattern = Pattern.compile("\\d+[.]\\d+E\\d");
    Matcher matcher = pattern.matcher(text);
    while(matcher.find()){

        replacementText = new BigDecimal(matcher.group()).toPlainString();
        matcher.appendReplacement(sb,replacementText);
        found = true;
        // System.out.println("Converted: " + matcher.group() + " to " + replacementText);
    }
    if ( found )
    {
        matcher.appendTail(sb);
        out = sb.toString();
        return out;
    }

    /*
     * 5.0E-4
     */
    pattern = Pattern.compile("\\d+[.]\\d+E[-+]\\d");
    matcher = pattern.matcher(text);
    while(matcher.find()){

        replacementText = new BigDecimal(matcher.group()).toPlainString();
        matcher.appendReplacement(sb,replacementText);
        // System.out.println("Converted: " + matcher.group() + " to " + replacementText);
    }
    matcher.appendTail(sb);
    out = sb.toString();
    return out;
}
user3613987
  • 119
  • 1
  • 2