简体   繁体   中英

java equivalent of atof C++

I want to convert a c++ (opencv) method to java (jaavcv) method

I'm worrying about this line . Is the conversion correct?

 float label=atof(entryPath.filename().c_str());

 --> float label=Float.parseFloat(child.getName());

Yes ,You are on track

float label = Float.parseFloat(child.getName());

where child.getName() is string and in valid float format

If you want it back

String floToStr= Float.toString(label );  

Update:

If you check docs ,

That method parseFloat

Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.

Throws:
NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable float.  

If child.getName() returns well-formatted String (for example 1.5 ) then yes, it's correct.

float label = Float.parseFloat(child.getName());

If child.getName() doesn't return parsable float (for example 1,5 ), parseFloat() will throw NumberFormatException .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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