numbers

Java String to complete mathematical sum

一世执手 提交于 2019-12-20 06:29:25
问题 In java, I am currently in the process of trying to find a way to do mathematical sums by typing in text to a JTextField, putting it into a String, and converting it to an int and solving. The problem is however, I don't want to simply put a number into the string but an actual sum including addition, subtraction etc. Currently it will accept just doing something like, 1 which will go from string and convert to an int, but when I do '1+1' or even just '1+' it throws exceptions everywhere

printing out prime numbers from array

老子叫甜甜 提交于 2019-12-20 06:28:48
问题 I'd like to print out all prime numbers from an array with method. I can do it with one int but don't know how to return certain numbers from array. Thanks for help! public static boolean isPrime(int [] tab) { boolean prime = true; for (int i = 3; i <= Math.sqrt(tab[i]); i += 2) if (tab[i] % i == 0) { prime = false; break; } for(int i=0; i<tab.length; i++) if (( tab[i]%2 !=0 && prime && tab[i] > 2) || tab[i] == 2) { return true; } else { return false; } //return prime; } thanks both of you.

Convert decimal number to vector

放肆的年华 提交于 2019-12-20 06:25:14
问题 In matlab I want to convert this: 12345.6788993442355456789 into a vector [1 2 3 4 5 6 7 8 8 9 9 3 4 4 2 3 5 5 4 5 6 7 8 9] I have found several solutions to convert a integer into a vector using commands like scanf, num2str,... but those don't fit with non-integers, and some solutions have problems with numbers with a large number of decimal places... That kind of conversion is needed because I want to see and use all of the digits of the number. 回答1: What input source are you getting those

Swift 2 - Search in string and sum the numbers

霸气de小男生 提交于 2019-12-20 05:43:05
问题 My old code was: let comps = split(str, { $0 == "-" || $0 == " " }, maxSplit: Int.max, allowEmptySlices: false) after update of Swift 2 my XCode 7 fix it to: let comps = split(str.characters, { $0 == "-" || $0 == " " }, maxSplit: Int.max, allowEmptySlices: false).map { String($0) } but now I have an error: Cannot invoke 'map' with an argument list of type '((_) -> _)' How to fix it. Link to old answer on Swift 回答1: The order of arguments for split() function messed up for some reason. It

Javascript using prototype how can I set the value of “this” for a number?

China☆狼群 提交于 2019-12-20 05:40:05
问题 So if we can get past the "should you?" question ... does anyone know how to set the value of an integer in prototype? Number.prototype.add = function(num){ var newVal = this.valueOf() + num; this.valueOf(newVal); return newVal; } var rad = 4001.23; document.write(rad.add(10) + '<br/>' + rad); You'll notice rad.add(10) returns the number contained in the variable "rad" plus 10, but I would really like to change the value of rad from within the prototype add function (something I know this

Check if variable is a number and positive integer in PHP?

这一生的挚爱 提交于 2019-12-20 05:15:06
问题 For example, say: <?php // Grab the ID from URL, e.g. example.com/?p=123 $post_id = $_GET['p']; ?> How do I check if variable $post_id is a number, and a positive integer at that (i.e. 0-9, not a floating point number, fraction, or a negative number)? EDIT: Can't use is_int 'cause $_GET returns a string. Think I need to use intval() or ctype_digit(), with the latter seeming more appropriate. For example: if( ctype_digit( $post_id ) ) { ... } 回答1: To check if a string input is a positive

need help converting numbers to word in Java

感情迁移 提交于 2019-12-20 05:11:30
问题 I'm working on a program that converts numbers to words, but I'm having problems with the toString() method in the Numbers class. All the methods were given to me, so I could implement; therefore, I can't remove any of them... number: 4564 --> four thousand and five hundred and sixty four here's the code Numbers class package numberstowords; import java.util.*; public class Numbers { //array containing single digits words numbers:0-9 private final String[] SINGLE_DIGITS_WORDS; //array

Odd behaviour of Python tokenizer when parsing integer numbers

让人想犯罪 __ 提交于 2019-12-20 04:19:33
问题 I noticed the following fact with CPython3 and Pypy3, contrasting with the behaviour of both CPython2 and Pypy2: In Python3, it looks like leading zeros when parsing code yield an error except for a very single number wich is 0 . Thus 00 is valid but not 042 . In Python2, leading zeros are allowed for all integers. Thus 00 and 042 are valid. Why did Python change its behaviour between both versions? 回答1: Python 3 standardized how all integer literals (other than base 10) were defined: 0?dddd.

Regex for DocumentFilter to match all decimal number but also number with just a decimal at the end

一笑奈何 提交于 2019-12-20 03:52:21
问题 Qestion First : I need to regex to match 111 or 111. or 111.111 (just aritrarty numbers) for a DocumentFilter . I need the user to be able to input 111. with a decimal and nothing afterwards. Can't seem to get it right. All the Regexes I find all match just all decimal numbers i.e. 12343.5565 32.434 32 Like this regex ^[0-9]*(\\.)?[0-9]+$ Problem is, I need the regex for a DocumentFilter so the input can only be numbers with/without a decimal point. But the catch is it also needs to match

Find groups of thousands which sum to a given number, in lexical order

允我心安 提交于 2019-12-20 03:44:16
问题 A large number can be comma formatted to read more easily into groups of three. E.g. 1050 = 1,050 and 10200 = 10,200 . The sum of each of these groups of three would be: 1050=1,050 gives: 1+50=51 10200=10,200 gives: 10+200=210 I need to search for matches in the sum of the groups of threes. Namely, if I am searching for 1234 , then I am looking for numbers whose sum of threes = 1234 . The smallest match is 235,999 since 235+999=1234 . No other integer less than 235,999 gives a sum of threes