parsing a float from web-service with JSON - web-services

to parse a NSString i have used this :
NSString *adress=[stations objectForKey:#"adress_station"];
what about a float, how should i call it, i try to do that :
float distance=[stations objectForKey:#"distance"];
but i have got an error :
incompatible type in initialization
i don't want to put it in NSString, i need it as float because i need it in other operations :)

float distance = [[stations objectForKey:#"distance"] floatValue];
objectForKey returns an object (in this case I assume an NSString).
Calling -floatValue on an NSString will get the data as a float.

Related

How to parse both Int and Float in JSON in Crystal?

Crystal JSON complains that it can't cast Int to Float, code below throws exception
JSON.parse("{\"v\":1}")["v"].as_f
How to parse it? In case when some values are Int "1" and some Floats "1.1"?
Crystal is comparatively strict with types here. You have to be more explicit.
Option 1.
Implement your own method. I'd recommend use a different name.
struct JSON::Any
def to_f : Float64?
case #raw
when Float64 then #raw.as(Float64)
when Int then #raw.as(Int).to_f
end
end
end
You can then call it as JSON.parse("{\"v\":1}")["v"].as_f
Option 2.
Inside your code do a check first.
my_v = JSON.parse("{\"v\":1}")["v"]
if my_v.as_f?
my_v.as_f
elsif my_v.as_i?
my_v.to_i.to_f
end

How to convert FText to float in UE4

I'm getting user input from a box in the unreal engine landscape import pane and would like to be able to convert that input to a float. Currently, the text comes in as FText
I've tried casting the resulting FText to float and using the built-in FText::toNumber.
I would like to be able to do something like:
FText mapDeltaX = GetPropertyValueText(PropertyHandle_Scale_X);
float deltaX = (float)mapDeltaX;
But unfortunately I get the error no suitable conversion from "FText" to "float" exists.
You can convert TCHAR* to float using
FCString::Atof(*String);
So in your case you'd convert your FText to FString, then to float:
FCString::Atof(*mapDeltaX.ToString());

How to convert structure to a string?

I have the following structure:
typedef struct {
float battery;
float other;
float humidity;
} rtcStore;
rtcStore rtcMem;
I need to send the data stored in the structure to thingspeak.com. However, to send the data I need to convert my structure to a string. Can anyone tell me how to do so? It will be more helpful if it's done in C.
You can use snprintf (https://linux.die.net/man/3/snprintf) as follow:
char buffer[100];
snprintf(buffer, 100, "%.4f %.4f %.4f", rtcMem.battery, rtcMem.other, rtcMem.humidity)
This will make sure that your message won't exceed 100 characters. Have a look at the documentation. You can also check the return value of snprintf to make sure everything went fine. See the example in the doc.
On the other side, you can parse the string using strtok to extract the fields and use a string to float convertor like strtof
Use sprintf to convert it to a string.

Converting a string to double using c_str() in C++

Is it not reccomended to convert a string in such way:
string input = "81.312";
double val = atof(input.c_str());
DO NOT use std::atof in C++. That doesn't check for input error.
Use std::stod. That checks for error also and throws exception accordingly.
Also, it takes std::string const & as argument. So you don't have to pass input.c_str(). Just do this:
double value = std::stod(input);
It is not wrong, but more right would be to use boost::lexical_cast.
You should also check if these tools handle NANs and INFs correctly.

Convert CString to float in mfc

how can I convert a CString variable to a floating point?
(I'm using visuall c++ 6.0 and the MFC)
I'm trying to use an edit box to return a value which I'm putting into an array of floating points. I'm Using the GetWindowText method to get the value, which returns a CString. So I need to convert to a floating point. (or am I just doing things completely the wrong way?).
I presume there are methods for doing this already in the MFC.(have already used the Format method to convet to a CString display the values in the array in the edit box)
Thanks.
you can just do
CString pi = "3.14";
return atof(pi);
EDIT
Also use this function:
CString pi = "3.14";
return _ttof(pi);
Reading a string value and parse/convert it to float allows you to locate the error when there is one. All you need is a help of a C Run-time function: strtod() or atof().
I would prefer strtod as the second argument returns a pointer to the string where the parse terminated:
CString str;
m_edtMyEditBox.GetWindowText(str);
char *pEnd;
double dValue = strtod(str.GetBuffer(str.GetLength()), &pEnd);
if (*pEnd != '\0')
{
// Error in parsing
}
str.ReleaseBuffer();