digits

How to find out if letter is Alphanumeric or Digit in Swift

感情迁移 提交于 2019-11-26 13:48:58
问题 I want to count the number of letters, digits and special characters in the following string: let phrase = "The final score was 32-31!" I tried: for tempChar in phrase { if (tempChar >= "a" && tempChar <= "z") { letterCounter++ } // etc. but I'm getting errors. I tried all sorts of other variations on this - still getting error - such as: could not find an overload for '<=' that accepts the supplied arguments 回答1: Update for Swift 3: let letters = CharacterSet.letters let digits =

Efficient way to determine number of digits in an integer

南笙酒味 提交于 2019-11-26 12:15:34
问题 What is a very efficient way of determining how many digits there are in an integer in C++? 回答1: Well, the most efficient way, presuming you know the size of the integer, would be a lookup. Should be faster than the much shorter logarithm based approach. If you don't care about counting the '-', remove the + 1. // generic solution template <class T> int numDigits(T number) { int digits = 0; if (number < 0) digits = 1; // remove this line if '-' counts as a digit while (number) { number /= 10;

Gets last digit of a number

早过忘川 提交于 2019-11-26 11:20:44
问题 I need to define the last digit of a number assign this to value. After this, return the last digit. My snippet of code doesn\'t work correctly... Code: public int lastDigit(int number) { String temp = Integer.toString(number); int[] guess = new int[temp.length()]; int last = guess[temp.length() - 1]; return last; } Question: How to solve this issue? 回答1: Just return (number % 10) ; i.e. take the modulus. This will be much faster than parsing in and out of a string. If number can be negative

How to allow introducing only digits in jTextField?

不想你离开。 提交于 2019-11-26 10:36:01
I have tried to use the example shown here but java showing error message of "AttributeSet cannot be resolved to a type" That is why I am trying to use another method of allowing only digits: txtUsername.addKeyListener(new MyKeyListener()); public class MyKeyListener extends KeyAdapter{ public void keyPressed(KeyEvent ke){ System.out.println("Key pressed code = "+ke.getKeyCode()); if (ke.getKeyCode()>=48 && ke.getKeyCode()<=57) return true; else return false; } } But of course it is not working because keyPressed method is void . So, what to do in order to print only digits in textfield? nIcE

Check if string contains only digits

☆樱花仙子☆ 提交于 2019-11-26 05:00:05
问题 I want to check if a string contains only digits. I used this: var isANumber = isNaN(theValue) === false; if (isANumber){ .. } But realized that it also allows + and - . Basically, I wanna make sure an input contains ONLY digits and no other characters. Since +100 and -5 are both numbers, isNaN() is not the right way to go. Perhaps a regexp is what I need? Any tips? 回答1: how about var isnum = /^\d+$/.test(val); 回答2: string.match(/^[0-9]+$/) != null; 回答3: String.prototype.isNumber = function()

How to allow introducing only digits in jTextField?

拟墨画扇 提交于 2019-11-26 02:14:15
问题 I have tried to use the example shown here but java showing error message of \"AttributeSet cannot be resolved to a type\" That is why I am trying to use another method of allowing only digits: txtUsername.addKeyListener(new MyKeyListener()); public class MyKeyListener extends KeyAdapter{ public void keyPressed(KeyEvent ke){ System.out.println(\"Key pressed code = \"+ke.getKeyCode()); if (ke.getKeyCode()>=48 && ke.getKeyCode()<=57) return true; else return false; } } But of course it is not

Controlling number of decimal digits in print output in R

跟風遠走 提交于 2019-11-26 01:25:38
问题 There is an option in R to get control over digit display. For example: options(digits=10) is supposed to give the calculation results in 10 digits till the end of R session. In the help file of R, the definition for digits parameter is as follows: digits: controls the number of digits to print when printing numeric values. It is a suggestion only. Valid values are 1...22 with default 7 So, it says this is a suggestion only. What if I like to always display 10 digits, not more or less? My

Sum the digits of a number - python

*爱你&永不变心* 提交于 2019-11-26 01:15:12
问题 If I want to find the sum of the digits of a number, i.e. : Input: 932 Output: 14 , which is (9 + 3 + 2) What is the fastest way of doing this? I instinctively did: sum(int(digit) for digit in str(number)) and I found this online: sum(map(int, str(number))) Which is best to use for speed, and are there any other methods which are even faster? 回答1: You can do it purely in integers, and it will be the most efficient: def sum_digits(n): s = 0 while n: s += n % 10 n //= 10 return s or with divmod

How to round a number to n decimal places in Java

六月ゝ 毕业季﹏ 提交于 2019-11-25 21:32:18
问题 What I would like is a method to convert a double to a string which rounds using the half-up method - i.e. if the decimal to be rounded is 5, it always rounds up to the next number. This is the standard method of rounding most people expect in most situations. I also would like only significant digits to be displayed - i.e. there should not be any trailing zeroes. I know one method of doing this is to use the String.format method: String.format(\"%.5g%n\", 0.912385); returns: 0.91239 which is