-1

I've been webscraping some some data from a web page, in this case I just need a exchange rate.

By webscraping I got a String with this info

¢559.41

But I just need a float number with this

559.41

My code looks like this (the variable datos can be used to save the float number)

public static void main(String [] args) throws IOException{
    float datos = 0;
    String tasa = null;
    Document d = Jsoup.connect("http://indi-eco.appspot.com/tcd").timeout(6000).get();
    Elements ele= d.select("span#lblRefSellMsg");
    tasa = (ele.text());
    
}}

2 Answers2

1

Kind of a duplicate of the question: java : convert float to String and String to float

But to answer your question: If the web element always has a currency token/symbol prefixed before the float number you want, you could simply parse the string by using something like:

tasa = ele.text().subString(1, ele.text().length());
datos = Float.parseFloat(tasa);

this would parse out the leading 1st character - the currency identifier - then attempt to take the float value from string and read into float variable data type.

Community
  • 1
  • 1
Nick Bell
  • 516
  • 3
  • 16
0

If the String contains text then numeric then you can use this regex to match or split

(([1-9]\d{0,2}(,\d{3})*)|(([1-9]\d*)?\d))(\.\d\d)?$

see https://regex101.com/r/PLioEH/1 and https://stackoverflow.com/a/17867041/2310289

and then pass the resulting string to Float.valueof (str);

Community
  • 1
  • 1
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64