How to convert NSString value #"3.45" into float. 3.45
float fCost = [[NSDecimalNumber decimalNumberWithString:#"3.45"]floatValue] ;
NSString *val = #"3.45";
float fCost = [val floatValue];
float number = [string floatValue];
NSString's: - (float)floatValue;
For example:
NSString *str = #"3.45";
float f = [str floatValue];
Reading your comment to Marek's answer it looks like your string actually = #"$3.45"
To convert that to a float use:
NSNumberFormatter * formatter = [[[NSNumberFormatter alloc] init] autorelease];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.currencyCode = #"USD";
float value = [[formatter numberFromString: #"$3.45"] floatValue];
Related
my code is below
NSNumber *someNumber = [[_dict2 objectForKey:#"location"] objectForKey:#"distance"];
NSString *someString = [someNumber stringValue];
_lbldistance.text=someString;
If you even want to include km in the string use NSLengthFormatter
NSNumber *someNumber = #1568;
NSLengthFormatter *formatter = [[NSLengthFormatter alloc] init];
NSString *distanceInKM = [formatter stringFromMeters:someNumber.doubleValue];
NSLog(#"%#", distanceInKM); // 1.568 km
That's rather simple: Divide by 1000.
By divide someNumber with 1000.
NSNumber *someNumber = [[_dict2 objectForKey:#"location"] objectForKey:#"distance"];
someNumber = [someNumber floatValue] * 1000;
NSString *someString = [someNumber stringValue];
1 meter is equal to 0.001 KM (as 1/1000 = 0.001).
So you can just multiply the value you receive from API with 0.001 and you will get the answer in KMs.
NSNumber *someNumber = [[_dict2 objectForKey:#"location"] objectForKey:#"distance"];
float convertedToKM = [someNumber floatValue]*0.001;
someNumber = [NSNumber numberWithFloat:convertedToKM];
NSString *someString = [someNumber stringValue];
_lbldistance.text=someString;
Since you are displaying the result, it would be better to limit it to two decimal places as is the standard practice. You can do it like:
NSNumber *someNumber = [[_dict2 objectForKey:#"location"] objectForKey:#"distance"];
float convertedToKM = [someNumber floatValue]*0.001;
NSString *someString = [NSString stringWithFormat:#"%.02f", convertedToKM];
_lbldistance.text=someString;
How can I get a simple comma every 3 digits in my result strings?
The code is as follows:
float convertFrom = [[_convertRates objectAtIndex:[picker selectedRowInComponent:0]] floatValue];
float convertTo = [[_convertRates objectAtIndex:[picker selectedRowInComponent:1]] floatValue];
float input = [inputText.text floatValue];
float to = convertTo;
float from = convertFrom;
float convertValue = input;
float relative = to / from;
float result = relative * convertValue;
NSString *convertFromName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:0]];
NSString *convertToName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:1]];
NSString *resultString = [[NSString alloc]initWithFormat:
#" %.4f %#",result, convertToName];
resultLabel.text = resultString;
NSString *formelString = [[NSString alloc]initWithFormat:
#" %.4f %#=", convertValue, convertFromName];
formelLabel.text = formelString;
This code makes a lot of digits, displayed in a block of text, which is not the most practical way to use the data. How can I implement commas in this code?
For example, 1234567 would be 1 234 567 or 1'234'567.
Use NSNumberFormatter.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *formattedString = [formatter stringFromNumber:someNumberValue];
where someNumberValue is an NSNumber object with the number.
This will format the number properly for the user's chosen region formatting.
You can control the number of digits after the decimal if you wish. See the docs for NSNumberFormatter for more details.
Do someone know if there is a method in order to round a float two numbers after the comme.
Examples :
10.28000 -> 10.3
13.97000 -> 14.0
5.41000 -> 5.4
Thanks a lot !
Regards,
Sébastien ;)
Use NSNumberFormatter to create a string with the specified faction digits.
float num = 10.28000;
formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
NSNumber *value = [NSNumber numberWithFloat:num];
NSString *newNumString = [formatter stringFromNumber:value];
Try this:
float aFloatValue = 3.1415926;
NSString *formatted = [NSString stringWithFormat:#"%01.1f", aFloatValue];
NSLog(#"Formatted: %#", formatted);
Think of it as a good old sprintf.
you could also try
float num = 10.28000
float result = (roundf(num * 10.0))/10.0;
(multiply by 10, then round, then divide by 10 again)
don't forget the .0 so that the division is by a float and not by an integer, which will round it again
The pure Cocoa way with NSRoundPlain behaviour:
- (NSDecimalNumber *)decimalNumberForFloat:(float)f_
{
NSNumber *number = [NSNumber numberWithFloat:f_];
NSDecimal decimal = [number decimalValue];
NSDecimalNumber *originalDecimalNumber = [NSDecimalNumber decimalNumberWithDecimal:decimal];
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
scale:0
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:NO];
return [originalDecimalNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
}
since NSDecimalNumber is a subclass of NSNumber, to obtain the primitive float call -[NSDecimalNumber floatValue]
Try this out:
float myFloat = 10.4246f;
myFloat = ((int) ((myFloat * 100) + 5)) / 100.0f;
float fval = 1.54f;
float nfval = ceilf(fval * 10.0f + 0.5f)/10.0f;
I have written this code and I want to format the number z as percent.
float l = ([textField2.text floatValue]);
float g = ([textField1.text floatValue]);
float x = l/1.23;
float y = x-g;
float z = y/l;
label.text = [[NSString alloc] initWithFormat:#"%2.2f \%",z];
Make your code as follows.
label.text = [[NSString alloc] initWithFormat:#"%2.2f %%",(z*100)];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:2]; //optional
....
NSNumber *number = [NSNumber numberWithFloat:0.435];
NSLog(#"%#", [numberFormatter stringFromNumber:number] );
43,50 %
You need to use %% in order to print a percent sign in a format string.
How do I convert a string to a float in Objective-C?
I am trying to multiply a couple of strings I am getting back from JSON:
float subTotal = [[[[purchaseOrderItemsJSON objectAtIndex:i] objectAtIndex:j] objectForKey:#"Price"] floatValue];
NSLog(#"%#", subTotal);
This gives me: (null) in the console. I know that there is a valid string coming out of that array because I am already using it to display its value in a label.
your_float = [your_string floatValue];
EDIT:
try this:
NSLog(#"float value is: %f", your_float);
The proper way of doing this is
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
float value = [numberFormatter numberFromString:#"32.12"].floatValue;