0

I'm a newbie at programming and maybe there is a solution to this problem I just don't know about. I have a TextView with values. As some of the values are really small they are shown as scientific numbers. But I don't like the 2e-02 expression I'd like to convert it to an Spannable which looks like 2*10-2. Is there an easy (already implemented way) or do I have to do a loop to count the number of decimal places (all my numbers are << 1) to create a html string?

It is just a question out of curiosity because I'm unable to find anything about this neither in books nor online. I don't have problems actually writing the function itself.

Darokthar
  • 1,033
  • 12
  • 18
  • 1
    Would it be easier to take the `2e-3` expression and parse it yourself? Ae-B => A*10[sup]B[/sup]? – rossum Sep 26 '11 at 11:13

1 Answers1

0

Assuming all your numbers are formatted like that, here’s how I would do it:

scientific.replace(/(.*)e-0*(.*)/, "$1 &times; 10<sup>&minus;$2</sup>")
Daniel Brockman
  • 18,826
  • 3
  • 29
  • 40
  • Yeah I did it your way, but I changed the regex a bit. Here is what I did: `String string = cursor.getString(cursor.getColumnIndex(index)); string = string.toLowerCase(); string = string.replaceFirst("(.*)e(.*)", "$1 &times $2"); textview.setText(Html.fromHtml(string));` I guess I have to read up about reg expressions. I never even thought about them. – Darokthar Sep 30 '11 at 19:46