numbers

regex compare two numbers

白昼怎懂夜的黑 提交于 2019-11-29 16:19:47
can i somehow compare two numbers in regex? i want regex that is correct for 10-12, but incorrect for 12-10. I mean that 10 must be smaller than 12. I want to do it in Javascript. If the input is always of the form X-Y, then why not use the split() function with '-' as the delimiter and then compare the two parts with > You can't compare numerical values using RegExps. I wouldn't use regex for this. I'd split the string on the operator, then compare the two resulting numbers based on what operator I found (I'm assuming 10+12 and 12+10 would both be legal). The problem here is that you're

extracting a number from a string in Java

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 15:29:13
I have a string with a number inside and I want to retrieve that number. for example if I have a string "bla bla 45 bla bla" I want to get the number 45. I have searched a bit and found out that this code should make the work Matcher matcher = Pattern.compile("\\d+").matcher("bla bla 45 bla bla"); if(matcher.matches()) String result = matcher.group(); but it doesn't :( probably the problem is that "\d+" regular expression is converted to "^\d+$" and so the matcher doesn't matches the number inside the text. Any ideas. You should use matcher.find() instead. Here's an example on how to use

Declaring a number in Python. Possible to emphasize thousand?

让人想犯罪 __ 提交于 2019-11-29 15:17:26
Is it possible to declare a number in Python as a = 35_000 a = 35,000 Neither seem to work of course. How do you emphasize things like these, for clarity in Python? Is it possible? This is actually just now possible in Python 3.6. You can use the first format that you showed: a = 35_000 because underscores are now an accepted separator in numbers. (You could even say a = 3_5_00_0 , though why would you?) The second method you showed will actually create a tuple. It's the same as saying: a = (35, 000) # Which is also the same as (35, 0). Yes, this is possible starting with python 3.6 . PEP 515

PHP integer part padding

烈酒焚心 提交于 2019-11-29 15:07:20
I need to pad the integer part with 0, the integer part must be at least 2 characters str_pad( 2 ,2,"0",STR_PAD_LEFT);// 02 -> works str_pad( 22 ,2,"0",STR_PAD_LEFT);// 22 -> works str_pad( 222 ,2,"0",STR_PAD_LEFT);// 222-> works str_pad( 2. ,2,"0",STR_PAD_LEFT);// 2. -> fails -> 02. or 02 str_pad( 2.11 ,2,"0",STR_PAD_LEFT);// 2.11-> fails -> 02.11 Is there simple code for that? If possible the same in Java please double x=2.11; String.format("%02d%s", (int) x, String.valueOf(x-(int) x).substring(1)) is not only ugly but prints 02.10999999999999988 edit for Java: Java integer part padding You

R: removing numbers at begin and end of a string

若如初见. 提交于 2019-11-29 14:54:24
I've got the following vector: words <- c("5lang","kasverschil2","b2b") I want to remove "5" in "5lang" and "2" in "kasverschil2" . But I do NOT want to remove "2" in "b2b" . gsub("^\\d+|\\d+$", "", words) #[1] "lang" "kasverschil" "b2b" Another option would be to use stringi library(stringi) stri_replace_all_regex(words, "^\\d+|\\d+$", "") #[1] "lang" "kasverschil" "b2b" Using a variant of the data set provided by the OP here are benchmarks for 3 three main solutions (note that these strings are very short and contrived; results may differ on a larger, real data set): words <- rep(c("5lang",

Mongodb PHP - Integers with decimals

这一生的挚爱 提交于 2019-11-29 14:51:42
This is a follow up to another post i made about inserting an integer into mongodb. I have this working just fine by using the (int) but now when it inserts the number into the db is strips everything after the decimal. So if i had 154.65 it would be inserted into the mongodb as 154 with nothing after the decimal. My question is how do i get this to keep all numbers after the decimal? Also if anyone has a link to these sting/numeric functions i'd appreciate a reference. Thanks. $number["xyz"] = (int) $_POST["number"] ; $collection->insert($number); This inserts it as a integer but strips

Numeric TextBox in C# - WPF [duplicate]

*爱你&永不变心* 提交于 2019-11-29 14:40:54
问题 This question already has an answer here: How do I get a TextBox to only accept numeric input in WPF? 30 answers i want to create a textbox in WPF that only accept numbers... i've reaserched and people say to use keypress event or masked textbox, but they are in windows forms... 回答1: For WPF: private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e) { if (!char.IsDigit(e.Text, e.Text.Length - 1)) e.Handled = true; } For Windows Forms: private void textBox1_KeyPress

In which case could “a != a” return “true”?

落爺英雄遲暮 提交于 2019-11-29 14:40:30
java.lang.Math#min(double, double) : public static double min(double a, double b) { if (a != a) return a; // a is NaN if (a == 0.0d && b == 0.0d && Double.doubleToLongBits(b) == negativeZeroDoubleBits) return b; return (a <= b) ? a : b; } In which case could a != a return true ? It seems that it's when a is NaN, but I can't imagine an example. Could you please provide one? A simple example is double d = Double.NaN; // or double d = 0.0/0.0; // or double d = Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY; if (Double.isNaN(a)) { // tests if a != a // do something BTW Double.compare() does

How to tell whether value is NaN without using isNaN, which has false positives? [duplicate]

荒凉一梦 提交于 2019-11-29 14:39:14
This question already has an answer here: How to test whether something is identically NaN? 3 answers How can I check whether the input value is NaN or not without using the isNaN function? If you can use ECMAScript 6 , you have Object.is : return Object.is(obj, NaN); Otherwise, here is one option, from the source code of underscore.js : // Is the given value `NaN`? _.isNaN = function(obj) { // `NaN` is the only value for which `===` is not reflexive. return obj !== obj; }; Also their note for that function: Note: this is not the same as the native isNaN function, which will also return true

扑克牌顺子

蹲街弑〆低调 提交于 2019-11-29 14:20:52
import java.util.Arrays; public class Solution { public boolean isContinuous(int[] numbers) { int numOfZero = 0; int numOfInterval = 0; int length = numbers.length; if(length == 0){ return false; } Arrays.sort(numbers); for (int i = 0; i < length - 1; i++) { // 计算癞子数量 if (numbers[i] == 0) { numOfZero++; continue; } // 对子,直接返回 if (numbers[i] == numbers[i + 1]) { return false; } numOfInterval += numbers[i + 1] - numbers[i] - 1; } if (numOfZero >= numOfInterval) { return true; } return false; } } numOfInterval += numbers[i + 1] - numbers[i] - 1; 这个计算2个数字之间的差值(这个差值是指的完成顺子) 例如 2 和 3满足顺子,相减结果为1