Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 356 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert an NSString into an NSNumber

#11
You can just use `[string intValue]` or `[string floatValue]` or `[string doubleValue] ` etc

You can also use [`NSNumberFormatter`][1] class:

[1]:

[To see links please register here]

Reply

#12
Thanks All! I am combined feedback and finally manage to convert from text input ( string ) to Integer. Plus it could tell me whether the input is integer :)

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:thresholdInput.text];

int minThreshold = [myNumber intValue];

NSLog(@"Setting for minThreshold %i", minThreshold);

if ((int)minThreshold < 1 )
{
NSLog(@"Not a number");
}
else
{
NSLog(@"Setting for integer minThreshold %i", minThreshold);
}
[f release];
Reply

#13
Worked in **Swift 3**

NSDecimalNumber(string: "Your string")
Reply

#14
I know this is very late but below code is working for me.

Try this code

`NSNumber *number = @([dictionary[@"keyValue"] intValue]]);`

This may help you. Thanks
Reply

#15
you can also do like this code 8.3.3 ios 10.3 support

[NSNumber numberWithInt:[@"put your string here" intValue]]
Reply

#16


extension String {

var numberValue:NSNumber? {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
return formatter.number(from: self)
}
}

let someFloat = "12.34".numberValue
Reply

#17
Try this

NSNumber *yourNumber = [NSNumber numberWithLongLong:[yourString longLongValue]];

Note - I have used longLongValue as per my requirement. You can also use integerValue, longValue, or any other format depending upon your requirement.
Reply

#18
Use an `NSNumberFormatter`:

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *myNumber = [f numberFromString:@"42"];

If the string is not a valid number, then `myNumber` will be `nil`. If it is a valid number, then you now have all of the `NSNumber` goodness to figure out what kind of number it actually is.
Reply

#19
Objective-C
==
(Note: this method doesn't play nice with difference locales, but is slightly faster than a `NSNumberFormatter`)

NSNumber *num1 = @([@"42" intValue]);
NSNumber *num2 = @([@"42.42" floatValue]);


___

Swift
==
**Simple but dirty way**

// Swift 1.2
if let intValue = "42".toInt() {
let number1 = NSNumber(integer:intValue)
}

// Swift 2.0
let number2 = Int("42")

// Swift 3.0
NSDecimalNumber(string: "42.42")

// Using NSNumber
let number3 = NSNumber(float:("42.42" as NSString).floatValue)


**The extension-way**
This is better, really, because it'll play nicely with locales and decimals.

extension String {

var numberValue:NSNumber? {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
return formatter.number(from: self)
}
}

Now you can simply do:

let someFloat = "42.42".numberValue
let someInt = "42".numberValue
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through