how to disable symbols after dot in double values - iphone

How to disable symbols after dot in double values?
13 + 3.456 = 16.456 GOOD
13 + 1 = 14.00 BAD! I need 14 how can I do It?
I am using NSMutableString.

if ([yourString doubleValue] % 1 == 0) {
[yourString setString:[NSMutableString stringWithFormat:#"%d", [yourString intValue]]];
}

Related

Objective C : Get correct float values(justified)

I worked a lot in it and can't find a solution. Even the title can't explain clearly.
I have three values weight, quantity and total
I had done the following
float wq = [[weightarray objectAtIndex:selectedint]floatValue];
float q = [quantity floatValue];
float total = wq * q;
for ex, if
[weightarray objectAtIndex:selectedint] = #"3.14";
quantity = 4;
then the result is
wq = 3.140000 q= 4.000000 total = 12.560000
but I need
wq = 3.14 total = 12.56
what to do?
I searched a lot, someone suggests to use NSDecimal,
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:FALSE raiseOnOverflow:TRUE raiseOnUnderflow:TRUE raiseOnDivideByZero:TRUE];
but the scale is not 2 here, wq value may have 3 or four numbers after point.
If the total = 2.30000100 means I need total = 2.300001
how to solve this?
I'm not entirely sure what it is your asking for, but it seems as if you want the values to only display a 2 d.p. In which case you could use a string format like so:
NSString *output = [NSString stringWithFormat:#"float = %.2f", 3.14];
The .2 specifies that the float should be justified to 2 d.p.
Hope this helps
There may be a more direct way to achieve it (which I don't know) but here's a suggestion...
Convert to string as you already do.
Use [myString hasSuffix:#"0"] to see if it ends in zero.
Use [myString substringToindex:[myString length]-1] to create a new string without the final zero.
Repeat.
I know it's not elegant, but unless someone has a better solution, this will at least do what you want.
UPDATE: scratch that - I just discovered [myString stringByTrimmingCharactersInSet:set]. Surely this must be what you need...?
Finally solution found, thanks to Martin
float total = 12.56000;
NSString *s = [NSString stringWithFormat:#"%f", total];
NSLog(#"%#",s);
BOOL success;
success =NO;
while(!success)
{
if ([s hasSuffix:#"0"])
{
s = [s substringWithRange:NSMakeRange(0,[s length]-1)];
}
else if ([s hasSuffix:#"."])
{
s = [s substringWithRange:NSMakeRange(0,[s length]-1)];
success = YES;
}
else
success = YES;
}
NSLog(#"%#",s);
if total = 12.560000 it returns total = 12.56
if total = 12.000000 it returns total = 12
if total = 10.000000 it returns total = 10
if total = 12.3000100 it returns total = 12.30001

Format UITextfield from UIPicker that has 5 components?

I have 1 UIPicker with 5 components, all numbers 1-9. I just want all 5 components to show up in the UITextfield. I have a feeling there is a much shorter code for this than what I have:
NSString *result = [metricMillimeterArray objectAtIndex:[metricMillimeterPicker selectedRowInComponent:0 & [metricMillimeterPicker selectedRowInComponent:1]& [metricMillimeterPicker selectedRowInComponent:2]& [metricMillimeterPicker selectedRowInComponent:3]& [metricMillimeterPicker selectedRowInComponent:4]]];
I can't tell what your objects are that are returned, but if
[metricMillimeterArray objectAtIndex:[metricMillimeterPicker selectedRowInComponent:0]
commands return strings (that are one character long) then you will need to:
result = [NSString stringWithFormat:#"%d mm",
10000 * [metricMillimeterPicker selectedRowInComponent:0] +
1000 * [metricMillimeterPicker selectedRowInComponent:1] +
100 * [metricMillimeterPicker selectedRowInComponent:2] +
10 * [metricMillimeterPicker selectedRowInComponent:3] +
[metricMillimeterPicker selectedRowInComponent:4] ];
This is assuming that your component 4 is your least significant, but the powers of 10 can be swapped if that part I got wrong.

Validating the phone numbers

I want to have only 13 numeric values or the 13numeric values can be prefixed with "+" sysmbol.so the + is not mandatory
Example : 1234567891234
another example is : +1234567891234
Telephone number format should be international,Is there any Regex for phone number validation in iPhone
I have tried the above link , but this +1234545 but i want to have only 13 numarals or + can be prefixed with that 13 numerals.
Please let me know , what can i change it here
This is the code i tried
NSString * forNumeric = #"^\\+(?:[0-9] ?){6,14}[0-9]$";
BOOL isMatch = [[textFieldRounded text] isMatchedByRegex:forNumeric];
if (isMatch == YES){
NSLog(#"Matched");
}
else {
NSLog(#"Not matched");
}
NSString * regex = #"((07|00447|004407|\\+4407|\\+447)\\d{9})";
Having found the leading 0 or the leading +44 once, why search for it again?
Basic simplification leads to
NSString * regex = #"((07|00440?7|\\+440?7)\\d{9})";
then to
NSString * regex = #"((07|(00|\\+)440?7)\\d{9})";
then to
NSString * regex = #"((0|(00|\\+)440?)7\\d{9})";
but 00 isn't the only common dial prefix, 011 is used in the US and Canada.
Adding that, and turning the order round, gives:
NSString * regex = #"(^((0(0|11)|\\+)440?|0)7\\d{9}$)";
or preferably
NSString * regex = #"(^(?:(?:0(?:0|11)|\\+)(44)0?|0)(7\\d{9}$))";
allowing 00447, 011447, +447, 004407, 0114407, +4407, 07 at the beginning, and with non-capturing groups.
For wider input format matching, allowing various punctuation (hyphens, brackets, spaces) use
NSString * regex = #"(^\\(?(?:(?:0(?:0|11)\\)?[\\s-]?\\(?|\\+)(44)\\)?[\\s-]?\\(?(?:0\\)?[\\s-]?\\(?)?|0)(7\\d{9})$)";
Extract the 44 country code in $1 (null if number entered as 07...) and the 10-digit NSN in $2.
However, be aware that numbers beginning 070 and 076 (apart from 07624) are NOT mobile numbers.
The final pattern:
NSString * regex = #"(^\\(?(?:(?:0(?:0|11)\\)?[\\s-]?\\(?|\\+)(44)\\)?[\\s-]?\\(?(?:0\\)?[\\s-]?\\(?)?|0)(7([1-5789]\\d{2}|624)\\)?[\\s-]?\\d{6}))$)";
Extract the NSN in $2 then remove all non-digits from it for further processing.
^(\+?)(\d{13})$ should do the trick, escape the slashes for objective-C usage.
13 digits, with an options + prefix.
If you want to play with regexp expressions you can use services like this one for visual feedback, very handy.
NSString * forNumeric = #"^(\\+?)(\\d{13})$";
How about this?
NSString *forNumeric = #"\\+?[0-9]{6,13}";
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:#"self matches %#", forNumeric];
BOOL isMatch = [predicate evaluateWithObject:#"+1234567890123"];
if (isMatch) NSLog(#"Matched");
else NSLog(#"Not matched");
NSDataDetector *matchdetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
error:&error];
NSUInteger matchNumber = [matchdetector numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
If you use UITextField then:
textField.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
you could try using a NSDataDetector:
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSDataDetector_Class/Reference/Reference.html
available in iOS4+
The following is what I do for validating UK mobile numbers:
- (BOOL) isValidPhoneNumber
{
NSString * regex = #"((07|00447|004407|\\+4407|\\+447)\\d{9})";
NSPredicate *testPredicate = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", regex];
BOOL validationResult = [testPredicate evaluateWithObject: self];
return validationResult;
}
See if it helps you

Converting decimal to binary

I want to convert decimal number in binary number. I'm using this method:
- (NSMutableString*)intStringToBinary:(long long)element{
NSMutableString *str = [[NSMutableString alloc] initWithString:#""];
for(NSInteger numberCopy = element; numberCopy > 0; numberCopy >>= 1)
{
[str insertString:((numberCopy & 1) ? #"1" : #"0") atIndex:0];
}
return str;
}
everything is going fine if the number "element" is >0. If the number is <0 there is the problem. For examle the method can't convert the number "-1". What can i do to solve the problem? Thanks in advance!!
You need an extra bit for the sign.
Example:
1xxxx represents the binary number + xxxx.
0yyyy represents the binary number - yyyy.
Here is a way to do it in Python using Wallar's Algorithm. The input and output are lists.
from math import *
def baseExpansion(n,c,b):
j = 0
base10 = sum([pow(c,len(n)-k-1)*n[k] for k in range(0,len(n))])
while floor(base10/pow(b,j)) != 0: j = j+1
return [floor(base10/pow(b,j-p)) % b for p in range(1,j+1)]

#'Event$variable' iphone Object C

$variable =1;
How i can do it?
I need insert variable 1 2 3 4 5 ...
in #"Event" or #'Event'
Stabbing in the dark here, but is this what you want?
int variable = 1;
NSString *str = [NSString stringWithFormat:#"Event%d", variable];