nawara
nawara

Reputation: 1167

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());

Upvotes: 1

Views: 2280

Answers (2)

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19837

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.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

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.  

Upvotes: 1

Related Questions