Is there a way to keep the leading zeros when converting a string to an integer. For example say the string was "01" is there a way i could store it as an integer value of 01?
- (int) getNextHand{
int temp = [[numbersArray objectAtIndex:cardsDelt] intValue];
NSLog(#"Card %i: %i", cardsDelt, temp);
cardsDelt++;
return temp;
}
My numbersArray contains 4 leading zero numbers they are:
"00"
"01"
"02"
"03"
If you want to log with leading zeroes use something like %02d
Nope integers are numbers and 01 will be automatically be converted to 1
01 and 1 are two representations of the same integer value. If the leading digits contain information, then you have a string, not an integer.
Related
I have column decimal(10,5) — VALUE, and column int — PICTURE.
So, I need to round VALUE to PICTURE digits after decimal point.
For example:
if VALUE = 10.50000 and PICTURE = 2, I want to get 10.50;
if VALUE = 0.15371 and PICTURE = 3, I want to get 0.154
If I use just Round() or Cast(Round(...) as nvarchar), then I have trailing zeros. If I Cast() to float, then I loose zeros.
Is there any solution?
You can use the str() function with trim():
select trim(str(value, 20, picture))
str() converts a number to a string with the specified length and precision. Sadly, it produces a fixed length string, left padded with spaces. The trim() removes the spaces.
Hi I want to smart round a float number, to keep the number of digits constant.
This number will always have 1 or 2 integer digits + undefined decimal digits.
I want to have a number with always 3 digits regardless of their position
EX:
83.235 = 83.2
0.110321 = 0.11
4.56723 = 4.57
If leading zeroes don't count as digits
NSNumberFormatter has usesSignificantDigits, minimumSignificantDigits, and maximumSignificantDigits properties that should do what you need. Set both values to 3, or just the max if you don't want trailing zeroes on values with fewer decimal places.
If you want leading zeroes to count as digits
I'm not aware of any built-in way of handling this, but you could write some custom logic to do it. e.g.
if (yourNum < 10)
formattedNum = [NSString stringWithFormat:#"%.2f", yourNum];
else
formattedNum = [NSString stringWithFormat:#"%.1f", yourNum];
This is assuming the number is always between 0 and 99.whatever, so if you're not 100% confident in the data integrity you'll probably want some more checks.
Hi have to convert an int value into an HEX string.
From 4 to 0x04
From -4 to 0xFC
I use this code [NSString stringWithFormat:#"0x%02X", x] where x is int.
With 4 I obtain 0x04, but with -4 I obtain 0xFFFFFFFC.
Where am I wrong ?
-4 of type int is indeed 0xFFFFFFFC on 32-bit systems. %02 will pad single-digit numbers with zero, but it will not truncate a longer number to two digits.
If you are interested in printing only the last eight bits, you need to mask the number yourself:
[NSString stringWithFormat:#"0x%02X", (x & 0xFF)];
I am making a binary to decimal number converter on iphone. having some problem when i trying to take each single digit from a number and do calculation. I tried char, characterAtIndex but they all failed to do calculation or i got the syntax completely wrong. Can anyone show me how to do such cast or there is an easier approach?
Your problem is getting numbers from strings?
The easiest way to get an integer from a character is to use the ascii table, like this:
NSString *stringOfNums = #"15";
char c;
int num;
for (int i = 0; i < [stringOfNums length]; ++i) {
c = [stringOfNums characterAtIndex:i];
num = c - 48; // 0 is 48 in ascii table
printf("\nchar is %c and num is %d", c, num);
}
The advantage of this method is that you can validate on a char-by-char basis that each falls in a range of 48 through 57, the ascii digits.
Or you could do the conversion in one step using NSNumberFormatter, as described here: How to convert an NSString into an NSNumber
As for the binary-decimal conversion, does your formula work on paper? Get that right first.
I need to convert integers into two decimal float numbers. So if i have an integer 3 i want it to be float 03. But if i have an two digit integer 33, i want it to be float 33.
The float value will always just be the actual number value. If you want to display that value and have to appear with a leading 0, you will have to use string formatting and the format options to produce "03" from a value of 3.