Roundoff number to greater value in Objective C - iphone

I want to roundoff my number to greater value. for example if i have 234 i want to make it 300 and 4436 to 5000. I have tried it but i can roundoff my value not in hundred and thousants but in tens. like i have a value 7771 it roundoff to 7900 or 7800 but i want it in 8000.
int temp = lroundf([[arrayPercentage objectAtIndex:i]floatValue]);
maxPer = (((temp + 10)/10))*10;

How about this:
-(void)roundUpNumber:(NSInteger) num {
NSInteger numLength = [[NSString stringWithFormat:#"%ld",num] length];
NSInteger newNum = ceil(num/pow(10,numLength-1)) * pow(10,numLength -1);
NSLog(#"%ld",newNum);
}

int num, count = 0;
int originalNumber = 7771;
num = originalNumber;
while (num) {
num = num/10;
count ++;
}
int power = pow(10,(count -1));
int firstDigit = originalNumber / power;
int finalNumber = (firstDigit + 1)* power;
NSLog(#"final result : %d",finalNumber);

Related

Flutter find the sum of digits of int value

I want to find the sum of the digits of the number entered in Flutter. I want to encode this algorithm.
for example
x=1992
result=1+9+9+2=21
how can i do this with flutter
You can do in this way.
import 'dart:io';
void main() {
print('Enter X');
int X = int.parse(stdin.readLineSync()!);
int result = 0;
for (int i = X; i > 0; i = (i / 10).floor()) {
result += (i % 10);
}
print('Sum of digits\n$result');
}
Output
Enter X
123456
Sum of digits
21
transform the number into an String using String stringValue = x.toString();
create an array from each char using List<String> result = stringValue.split('');
sum each number transforming back using int.parse(result)
void main(){
int x = 1992;
String stringValue = x.toString();
List<String> result = stringValue.split('');
int sum = 0;
for(int i = 0 ; i < result.length; i++) {
int value = int.parse(result[i]);
sum = sum + value;
}
print(sum);
}
Result: 21

How to count digits in BigDecimal?

I’m dealing with BigDecimal in Java and I need to make 2 check against BigDecimal fields in my DTO:
Number of digits of full part (before point) < 15
Total number of
digits < 32 including scale (zeros after point)
What is the best way to implement it? I extremely don’t want toBigInteger().toString() and .toString()
I think this will work.
BigDecimal d = new BigDecimal("921229392299229.2922929292920000");
int fractionCount = d.scale();
System.out.println(fractionCount);
int wholeCount = (int) (Math.ceil(Math.log10(d.longValue())));
System.out.println(wholeCount);
I did some testing of the above method vs using indexOf and subtracting lengths of strings. The above seems to be signficantly faster if my testing methodology is reasonable. Here is how I tested it.
Random r = new Random(29);
int nRuns = 1_000_000;
// create a list of 1 million BigDecimals
List<BigDecimal> testData = new ArrayList<>();
for (int j = 0; j < nRuns; j++) {
String wholePart = r.ints(r.nextInt(15) + 1, 0, 10).mapToObj(
String::valueOf).collect(Collectors.joining());
String fractionalPart = r.ints(r.nextInt(31) + 1, 0, 10).mapToObj(
String::valueOf).collect(Collectors.joining());
BigDecimal d = new BigDecimal(wholePart + "." + fractionalPart);
testData.add(d);
}
long start = System.nanoTime();
// Using math
for (BigDecimal d : testData) {
int fractionCount = d.scale();
int wholeCount = (int) (Math.ceil(Math.log10(d.longValue())));
}
long time = System.nanoTime() - start;
System.out.println(time / 1_000_000.);
start = System.nanoTime();
//Using strings
for (BigDecimal d : testData) {
String sd = d.toPlainString();
int n = sd.indexOf(".");
int m = sd.length() - n - 1;
}
time = System.nanoTime() - start;
System.out.println(time / 1_000_000.);
}

How to convert a binary number into a decimal fraction in dart?

Hi i have been wondering if there is a way in which to convert binary numbers into decimal fractions.
I know how to change base as an example through this code
String binary = "11110010";
//I'd like to change this line so it produces a decimal value
String denary = int.parse(binary, radix: 2).toRadixString(10);
If anyone still wondering how to convert decimal to binary and the inverse:
print(55.toRadixString(2)); // Outputs 110111
print(int.parse("110111", radix: 2)); Outputs 55
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
int main()
{
int num = 10101001;
cout << binaryToDecimal(num) << endl;
}
This is my c++ solution but you can implement any language

how to check the greater value in iphone application

I am creating graph i get 5 values at run time so how should i know which is greater so that i may set max limit
I have following 5 varibale of values;
int value3=(int)roundf(appDelegate.itemOneValue);
int value4=(int)roundf(appDelegate.itemTwoValue);
int value5=(int)roundf(appDelegate.itemThreeValue);
int value6=(int)roundf(appDelegate.itemFourValue);
int value7=(int)roundf(appDelegate.itemFiveValue);
how to check of the above varible which has larger value.
NSArray *allValues = #[[NSNumber numberWithInt:(int)roundf(appDelegate.itemOneValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemTwoValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemThreeValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemFourValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemFiveValue)]];
int max = [[allValues valueForKeyPath:#"#max.intValue"] intValue];
or
NSNumber * max = [allValues valueForKeyPath:#"#max.intValue"];
An alternative that is less flexible but may be easier to understand:
int max = (value2 < value1) value2 : value1;
max = (value3 < max) value3 : max;
max = (value4 < max) value4 : max;
max = (value5 < max) value5 : max;
max = (value6 < max) value6 : max;
max = (value7 < max) value7 : max;
// now max contains the highest of all values.
You can read a great article on wikipedia: http://en.wikipedia.org/wiki/Selection_algorithm
Here the pseudo-code to get min/max from an array:
function select(list[1..n], k)
for i from 1 to k
minIndex = i
minValue = list[i]
for j from i+1 to n
if list[j] < minValue
minIndex = j
minValue = list[j]
swap list[i] and list[minIndex]
return list[k]
Algorithm for max and min? (Objective-C)
http://en.wikipedia.org/wiki/Maxima_and_minima
How would you define a simple "min" method in obj-c
http://goo.gl/BO5Qx
In objc, the pseudo code became something like this:
int value3=(int)roundf(appDelegate.itemOneValue);
int value4=(int)roundf(appDelegate.itemTwoValue);
int value5=(int)roundf(appDelegate.itemThreeValue);
int value6=(int)roundf(appDelegate.itemFourValue);
int value7=(int)roundf(appDelegate.itemFiveValue);
NSArray *values = #[[NSNumber numberWithInt:value3],
[NSNumber numberWithInt:value4],
[NSNumber numberWithInt:value5],
[NSNumber numberWithInt:value6],
[NSNumber numberWithInt:value7]];
NSNumber min=0;
NSNumber max=0;
for ( int i=0; i<[values count]; i++ ) {
min = [values objectAtIndex:i];
//etc...
for (int j=0; j<i+1; j++ ) {
if ( [values[j] > min] ) {
// etc..
}
}
}
hope this helps.

Split a integer into its separate digits

Say I have an integer, 9802, is there a way I can split that value in the four individual digits : 9, 8, 0 & 2 ?
Keep doing modulo-10 and divide-by-10:
int n; // from somewhere
while (n) { digit = n % 10; n /= 10; }
This spits out the digits from least-significant to most-significant. You can clearly generalise this to any number base.
You probably want to use mod and divide to get these digits.
Something like:
Grab first digit:
Parse digit: 9802 mod 10 = 2
Remove digit: (int)(9802 / 10) = 980
Grab second digit:
Parse digit: 980 mod 10 = 0
Remove digit: (int)(980 / 10) = 98
Something like that.
if you need to display the digits in the same order you will need to do the module twice visa verse this is the code doing that:
#import <Foundation/Foundation.h>
int main (int argc, char * argv[])
{
#autoreleasepool {
int number1, number2=0 , right_digit , count=0;
NSLog (#"Enter your number.");
scanf ("%i", &number);
do {
right_digit = number1 % 10;
number1 /= 10;
For(int i=0 ;i<count; i++)
{
right_digit = right_digit*10;
}
Number2+= right_digit;
Count++;
}
while ( number != 0 );
do {
right_digit = number2 % 10;
number2 /= 10;
Nslog(#”digit = %i”, number2);
}
while ( number != 0 );
}
}
return 0;
}
i hope that it is useful :)