简体   繁体   English

如何从字符串中提取浮点数字数据

[英]How to Extract float numeric data from a string

Hey Guys i am using Google Currency Api to request for currency conversions information. 大家好,我正在使用Google Currency Api要求提供货币兑换信息。 For example i use Google Currency Api 例如,我使用Google Currency Api

to convert 1USD to my local Currency. 将1USD转换为我的本地货币。 The string returned is {lhs: "1 US dollar",rhs: "2 481.38958 Ugandan shillings",error: "",icc: true} I need java code to extract the 2481.38958 float data type and save it in a float Variable. 返回的字符串是{lhs:“ 1美元”,rhs:“ 2 481.38958乌干达先令”,错误:“”,icc:true}我需要Java代码来提取2481.38958 float数据类型并将其保存在float变量中。 Please Help. 请帮忙。 Thanks alot. 非常感谢。

For your input JSON string: 对于您输入的JSON字符串:

{lhs: "1 U.S. dollar",rhs: "2481.38958 Ugandan shillings",error: "",icc: true}

Using http://json-lib.sourceforge.net/ : 使用http://json-lib.sourceforge.net/

    JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );        
    String dollarString = json.getFloat( "rhs" );
    float dollars = Float.parseFloat(dollarString.split(" ")[0]);

This string is at JSON format. 此字符串为JSON格式。 There are libs for manipulate this as an object. 有用于将其作为对象进行操作的库。 Examples : GSON ( http://code.google.com/p/google-gson/ ) or http://www.json.org/java/ 范例:GSON( http://code.google.com/p/google-gson/ )或http://www.json.org/java/

Something like : new JSONObject("{lhs: "1 US dollar",rhs: "2 481.38958 Ugandan shillings",error: "",icc: true}").get("rhs") 类似于:new JSONObject(“ {lhs:” 1美元“,rhs:” 2 481.38958乌干达先令“,error:”“,icc:true}”)。get(“ rhs”)

And after you have to suppress unit, maybe with a regexp. 在您必须抑制单位之后,也许使用正则表达式。 And finally... Float.parseFloat("2 481.38958") 最后... Float.parseFloat(“ 2 481.38958”)

Considering the value always will be between rhs: and a word. 考虑值始终在rhs:和一个字之间。

String str = "{lhs: \"1 U.S. dollar\",rhs: \"2 481.38958 Ugandan shillings\",error: \"\",icc: true}";
Matcher m = Pattern.compile("rhs:\\s.*?([\\d\\s\\.]+)\\s\\w+").matcher(str);
m.find();
float value = Float.parseFloat(m.group(1).replaceAll("[^\\d\\.]", ""));
System.out.println(value);

If the response always contains the same pattern (with Ugandan shillings text), one possible way of doing it is something like this: 如果响应始终包含相同的模式(带有乌干达先令文本),则一种可能的处理方式如下所示:

package so;

import java.util.StringTokenizer;

public class DemoString {

    public static void main(String[] args) {
        String s = new String("{lhs: \"1 U.S. dollar\",rhs: \"2 481.38958 Ugandan shillings\",error: \"\",icc: true}") ;
        StringTokenizer st = new StringTokenizer(s, "\"");
        st.nextToken();  //{lhs: 
        st.nextToken();  //1 U.S. dollar
        st.nextToken();  //,rhs: 
        String value = st.nextToken();  //2 481.38958 Ugandan shillings
        String num = value.substring(0, value.indexOf("U"));  // 2 481.38958 
        num = num.replaceAll(" ", "");
        Float fnum = 0f;
        try {
            fnum = Float.parseFloat(num);           
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        System.out.println("The float number is: " + fnum.toString());
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM