I am trying to convert float value to string. For that i used 'ftoa' but it shows error like 'undefined reference to ftoa()'.Also i used 'gcvt()' instead of ftoa but not working.Any suggestion please?
Thanks.
if it is good enough for you :
https://github.com/antongus/stm32tpl/blob/master/ftoa.c
You could use sprintf. Not very efficient though.
Related
How to evaluate a string which could be changed dynamically in code? for example:
A=rand(60, 60);
RangeC='10:end,:';
B=A(RangeC);
I know this is quite easy for others, but I have struggled for hours! Thanks in advance!
You can use the eval function but I would suggest to seperate RangeC in two variables like the example below. Also end won't be able to be evaluated so you can use size instead.
A=rand(60, 60);
RangeC1='10:size(A,1)';
RangeC2='1:size(A,2)';
B=A(eval(RangeC1), eval(RangeC2));
Facebook's code changes on Tuesday night have impacted how parseInt works in FBJS. Where I previously used it to convert decimal numbers to straight integers, now it always returns undefined.
For example:
return parseInt(decimalnum);
no longer works. Anyone figured out how we are supposed to round to integers now? Thanks.
Thanks for the report. It's fixed on trunk now; it should be out tomorrow unless there's another push later today.
I suspect that decimalnum is not defined in your function. Try replacing your return with return decimalnum; -- you may still be returning undefined.
parseInt is not for rounding - it actually takes the integer component of a number, or coerces a string to be a number. If you want to round, use Math.round. Depending on your usage, you may find Math.floor or Math.ceil useful.
Math.floor()
Math.ceil()
Math.round()
parseInt()
Did you try parseInt(decimalnum, 10); ?
I am new to coding and objective C so thanks for the help in advance.
I have a .plist file containing an array of strings filled with formulas such as
*5.3
/2
-10.5
I am able to retrieve these string values from the .plist file but I am getting a little stuck trying to append these string formulas to existing variables with the hopes of returning a converted number. For example I would like to use my variable 7 with the formula *5.3 and return 37.1
7 *5.3 -> 37.1
Any help would be greatly appreciated
Appending the string to a variable is straightforward; it can be accomplished with something like this:
NSString *equation = [NSString stringWithFormat:#"%d%#", variable, plistEntry];
You'll run into problems when you want to evaluate this equation, however. This SO question discusses expression evaluation in Objective-C. Dave DeLong's answer links to a couple of libraries that you may want to look into: DDMathParser and GCMathParser.
This can't be done as-is. You'll need one of the many free expression evaluators (probably in C) that float around on the web.
See this SO question.
I am using the pow() function like this in my application:
float IValuePlusOne = (pow(2.25,120));
but it is reporting back an infinite value, instead of the correct result. I even tried the long long and long double data types but couldn't get them to produce the proper output.
Is there any other data type I need to use or do I need to make some other changes in my code?
As others have pointed out, you're losing precision and reducing the size of value you can represent by casting to a float. Running the following code:
double IValuePlusOne = pow(2.25,120.0);
NSLog(#"Test value: %f", IValuePlusOne);
on my iPhone gives the output:
Test value: 1827688475348373523156051712429585892114432.000000
which looks to be correct (1.827x10^42).
If you want to do calculations on values that a double can't hold, use NSDecimalNumber.
Is there any reason why the asterisk is next to the object type in this code? I'm a little confused by the way I see this used. Some times it looks like this:
NSString* stringBefore;
and sometimes like this:
NSString *stringBefore;
Is there a difference? Or a right or wrong way to do this?
Thanks
I use the * near the variable name and not the type, since if you declare something like:
int *i, j;
i will be a pointer to int, and j will be a int.
If you used the other syntax:
int* i, j;
you may think that both i and j are pointers when they are not.
That said, I don't use nor recommend declaring a pointer and a non-pointer variable in the same line, as in this sample.
It makes no difference.
It just just an indicator to how well versed the author is in writing and reading Objective-C. The traditional standard is to write it as:
NSString *stringBefore;
There is no difference.