Divide large number by 1000 and round to one decimal place - ios

I am finding it very difficult to do a simple operation, take a large number (4 digits or above) divide by 1000, round to one decimal place and then display as a string with K but perhaps I am missing an obvious answer. (There are tons of questions on this on SO but no one seems to agree on a good answer.)
I would like the following to display as 5.6K.
int startingint = 5654;
int formatted = startingint/1000;
NString *formattedstr = [NSString stringWithFormat:#"%dK", formatted];
Instead, it displays 5K.
Can anyone suggest how to get it to show an extra decimal place?
Thanks in advance for any suggestions.

You can try
float starting = 5654.0;
float formatted = starting/1000.0;
NString *formattedstr = [NSString stringWithFormat:#"%.1fK", formatted];

Related

Lua random number to the 8th decimal place

How do I get a random number in Lua to the eighth decimal?
Example : 0.00000001
I have tried the following and several variations of this but can not get the format i need.
math.randomseed( os.time() )
x = math.random(10000000,20000000) * 0.00000001
print(x)
i would like to put in say 200 and get this 0.00000200
Just grab a random number from 0-9, and slide it down 6 places. You can use format specifiers to create the string representation of the number that you desire. For floats we use %f, and indicate how many decimal places we want to have with an intermediate .n, where n is a number.
math.randomseed(os.time())
-- random(9) to exclude 0
print(('%.8f'):format(math.random(0, 9) * 1e-6))
--> '0.00000400'
string.format("%.8f",math.random())
to help anyone else. my question should have been worded a bit better. i wanted to be able to get random numbers and get it to the 8th decimal place.
but i wanted to be able to have those numbers from 1-10,000 so he is updated how i wanted it and the help of Oka got me to this
math.randomseed(os.time())
lowest = 1
highest = 7000
rand=('%.8f'):format(math.random(lowest, highest) / 100000000)
print(rand)
Hope this helps someone else or if it can be cleaned up please let me know

Always display a certain number of digits

I am creating a calculator app. For aesthetic reasons (numbers need to line up with an image), the result always needs to be five digits long, even if the first four digits are spaces or zeros. For example, the user enters 1+1, the result is 2, but my app should display it as "00002" or " 2".
How can I achieve this?
Well, you contradict yourself when you say the number should always be five digits long then say your app should display it as "00002" or "2". I'll assume you actually want the former? Here's a simple example assuming your number is int x.
int x = 2;
NSString *string = [NSString stringWithFormat:#"%05d", x];

Hex to Dec code calculation

I have two cards which contains a hex value, I am struggling to find out what kind of algorithm is used ti get the decimal value.
8HEX from chip: 0b98c44a Printed on card: 3491308370
8HEX from chip: 0c96425c Printed on card: 812204602
does any one of you number experts in here find out how it is done, you helped me with this before :)
The relation between the numbers seems to be that the bits of every byte has been reversed.
When you print first pair in binary they are:
11010000 00011001 00100011 01010010 = 3491308370
00001011 10011000 11000100 01001010 = 0x0b98c44a
The second pair is:
00110000 01101001 01000010 00111010 = 812204602
00001100 10010110 01000010 01011100 = 0x0c96425c
If you want to know how to convert one number to the other, you should mention which programming language you are using.

iOS: Find out number of fraction digits [duplicate]

This question already has answers here:
digits after decimal point
(2 answers)
How to calculate number of digits after floating point in iOS?
(3 answers)
Closed 9 years ago.
I have some numbers that comes from server, how to know how many fraction digits in number ?
I mean how to know that 2.43 has 2 numbers after coma, 3.145 - 3 numbers, 2.0003 - 4 numbers.
Thanks in advance..
Assuming you are using a number, change it to a string. Remove the decimal point and get the last object and the length.
Example:
NSNumber *number = [NSNumber numberWithFloat:3.902];
[number.stringValue componentsSeparatedByString: #"."].lastObject.length;
I would do this :
NSNumber *number = [[NSNumber alloc] initWithFloat:3.902];
NSUInteger i = [[number stringValue] rangeOfString:#"."].location;
long numberOfDigits = [[number stringValue] length]-(i+1);
NSLog(#"%ld", numberOfDigits);
3
convert to string.
find the decimal point range.
subtract string length from the decimal point position and Add one.
Do you mean that you are retrieving the data from a server in a stream?
If your getting the data as a stream, it should be prepended with the length of the string, that is how you would know how many places to expect.
So on the server, first convert to string, then determine the length, or use another method suggested, then prepend the length of the string, and then send that number first.

Round floating value to .1 (tens place) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I round a float value to 2 post decimal positions?
Lets say I have a double number of 3.46.
How do I round it to 3.50?
I tried
NSLog(#"res: %.f", round(3.46));
but it return 3.
Do some calculations....
float f=3.46;
float num=f+0.05;//3.51
int intNum=num*10;//35
float floatNum=intNum/10.0;//3.5
NSLog(#"res: %.2f", floatNum); //3.50
Following code may help
i = roundf(10 * i) / 10.0;
where i is your float variable
If you're willing to live with the rounding rules from printf, then the following should suffice when rounding for presentation:
NSLog(#"res: %.1f0", 3.46);
Note that the 0 is just a normal character that is added after the value is formatted to the appropriate number (1) of decimal places. This approach should be usable with [NSString stringWithFormat:] as well.
The original code results in "3" because round always returns an integral value.
YMMV; I don't even use iOS.

Resources