integer

How to convert NSTimeInterval to int?

寵の児 提交于 2019-12-30 05:32:05
问题 How do I convert NSTimeInterval into an Integer value? My TimeInterval holds the value 83.01837 . I need to convert it into 83 . I have googled but couldn't find any help. 回答1: Direct assignment: NSTimeInterval interval = 1002343.5432542; NSInteger time = interval; //time is now equal to 1002343 NSTimeInterval is a double, so if you assign it directly to a NSInteger (or int, if you wish) it'll work. This will cut off the time to the nearest second. If you wish to round to the nearest second

Swift How to get integer from string and convert it into integer

淺唱寂寞╮ 提交于 2019-12-29 20:10:22
问题 I need to extract numbers from string and put them into a new array in Swift. var str = "I have to buy 3 apples, 7 bananas, 10eggs" I tried to loop each characters and I have no idea to compare between Characters and Int. 回答1: First, we split the string so we can process the single items. Then we use NSCharacterSet to select the numbers only. import Foundation let str = "I have to buy 3 apples, 7 bananas, 10eggs" let strArr = str.split(separator: " ") for item in strArr { let part = item

How to get the Power of some Integer in Swift language?

眉间皱痕 提交于 2019-12-29 10:34:44
问题 I'm learning swift recently, but I have a basic problem that can't find an answer I want to get something like var a:Int = 3 var b:Int = 3 println( pow(a,b) ) // 27 but the pow function can work with double number only, it doesn't work with integer, and I can't even cast the int to double by something like Double(a) or a.double()... Why it doesn't supply the power of integer? it will definitely return an integer without ambiguity ! and Why I can't cast a integer to a double? it just change 3

How to get the Power of some Integer in Swift language?

落爺英雄遲暮 提交于 2019-12-29 10:34:38
问题 I'm learning swift recently, but I have a basic problem that can't find an answer I want to get something like var a:Int = 3 var b:Int = 3 println( pow(a,b) ) // 27 but the pow function can work with double number only, it doesn't work with integer, and I can't even cast the int to double by something like Double(a) or a.double()... Why it doesn't supply the power of integer? it will definitely return an integer without ambiguity ! and Why I can't cast a integer to a double? it just change 3

Get integer value from another class

好久不见. 提交于 2019-12-29 09:24:08
问题 I know i have done this before, but I just cant remember how to do it. I have a integer, that i want to be able to change in another class of mine. how do i do it? MainViewClass : UIViewController { int score; } #import "MainViewClass.h" OtherClass : MainViewClass{ } Then in the .m of OtherClass i want to be able to use the variable score. How do i do this? I have searched around the internet, and have tried several things to try to get it to work will no success. Thanks for looking! Have a

What's the correct way to test if a variable is a number in PHP?

时间秒杀一切 提交于 2019-12-29 08:47:11
问题 When I fetch data from a database, the result is a string even if it has an number value. Here's what I mean: // An integer $int=10; if(is_int($int)) // Returns true ... // A string $int='10'; if(is_int($int)) // Returns false ... I want both of these to return true. 回答1: Use is_numeric() if you want it to accept floating point values, and ctype_digit() for integers only. 回答2: You're looking for is_numeric(). http://php.net/is_numeric 回答3: You can not enclose a variable with quote (single or

Signed Integer Network and Host Conversion

本小妞迷上赌 提交于 2019-12-29 06:17:35
问题 I would like to convert a int32_t from host byte order to network byte order and vice versa. I know about the htonl() function and its variants, but this takes unsigned integers. Is there a standard library function which can do the same with signed integers or do I have to implement it myself? And if I have to implement it myself, how should I do it? I'm looking to find a routine that will work on Linux and Mac OS X. 回答1: It does not matter. htonl is concerned with bytes, not with

Finding a Specific Digit of a Number

北慕城南 提交于 2019-12-29 06:17:14
问题 I'm trying to find the n th digit of an integer of an arbitrary length. I was going to convert the integer to a string and use the character at index n... char Digit = itoa(Number).at(n); ...But then I realized the itoa function isn't standard. Is there any other way to do this? 回答1: (number/intPower(10, n))%10 just define the function intPower . 回答2: You can also use the % operator and / for integer division in a loop. (Given integer n >= 0, n % 10 gives the units digit, and n / 10 chops off

Perfect square and perfect cube

女生的网名这么多〃 提交于 2019-12-29 04:23:25
问题 Is there any predefined function in c++ to check whether the number is square of any number and same for the cube.. 回答1: No, but it's easy to write one: bool is_perfect_square(int n) { if (n < 0) return false; int root(round(sqrt(n))); return n == root * root; } bool is_perfect_cube(int n) { int root(round(cbrt(n))); return n == root * root * root; } 回答2: sqrt(x) , or in general, pow(x, 1./2) or pow(x, 1./3) For example: int n = 9; int a = (int) sqrt((double) n); if(a * a == n || (a+1) * (a+1

Integer.parse(String str) java.lang.NumberFormatException: Errors

喜欢而已 提交于 2019-12-29 01:50:23
问题 I keep getting number format expectations, even though I'm trimming the strings and they don't contain non numerical characters bizarrely it works for some numbers and not others. Below is an example of a string I get number format exception for. Also, any string starting with 0 e.g "0208405223", is returned 208405223, there's no zero anymore is that supposed to happen? String n="3020857508"; Integer a = Integer.parseInt(n.trim()); System.out.println(a); This is the exception: Exception in