numbers

Check if a variable is a natural number

偶尔善良 提交于 2019-11-30 05:57:46
问题 I want to make a bid system on a website. That means users can post their bid (natural number). I want to make sure users don't try to post characters, decimal numbers, etc. I don't want to use is_numeric function because hexadecimal notation is allowed. I was thinking to use preg_match for this. But in php.net the documentation for this function is little and I have no idea how to use preg_match. So how should I check if a variable is a natural number with preg_match? 回答1: If you don't

Best way to restrict a text field to numbers only?

瘦欲@ 提交于 2019-11-30 05:55:49
问题 I'm using the following Javascript to restrict a text field on my website to only accept numerical input, and no other letters or characters. The problem is, it REALLY rejects all other key inputs, like ctrl - A to select the text, or even any other browser functions like ctrl - T or ctrl - W while the text box is selected. Does anyone know of a better script to only allow numerical input, but not block normal commands (that aren't being directly input into the field)? Thanks Here is the code

Generating all distinct partitions of a number

女生的网名这么多〃 提交于 2019-11-30 05:37:20
I am trying to write a C code to generate all possible partitions (into 2 or more parts) with distinct elements of a given number. The sum of all the numbers of a given partition should be equal to the given number. For example, for input n = 6 , all possible partitions having 2 or more elements with distinct elements are: 1, 5 1, 2, 3 2, 4 I think a recursive approach should work, but I am unable to take care of the added constraint of distinct elements. A pseudo code or a sample code in C/C++/Java would be greatly appreciated. Thanks! Edit: If it makes things easier, I can ignore the

Why are two different numbers equal in JavaScript?

廉价感情. 提交于 2019-11-30 05:35:42
I've been messing around with a JavaScript console, when I suddenly decided to try this: 0x100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF Surprisingly, they're equal: Why does it happen? They're clearly different numbers (even the 0xFFFF...FFFF is one digit shorter) If I add a F to the 0xFFFF...FF , they're not equal anymore:

Default php function that turns negative numbers in 0

∥☆過路亽.° 提交于 2019-11-30 05:35:13
Is there such a thing? for eg $var = -5; echo thefunction($var); // should be 0 $var = 5; echo thefunction($var); // should be 5 Try max($var,0) , which will have the desired effect. See the manual page for more information. Not built-in but, here you have: function thefunction($var){ return ($var < 0 ? 0 : $var); } Hope this helps In PHP, checking if a integer is negative and if it is then setting it to zero is easy, but I was looking for something shorter (and potentially faster) than: if ($x < 0) $x = 0; Well, this is a very quick check and reset, but there is a function max that does this

Fastest way to get number of digits on a number? [duplicate]

喜你入骨 提交于 2019-11-30 05:05:55
This question already has an answer here: Way to get number of digits in an int? 28 answers I have do detect the amount of digits on a number. For example, 329586 has 6 digits. What I done, is simply parsing the number to string, and getting the string length, like: number.toString().length() But, is there a fastest way to count digits on a number? I have to use this method several times, so I think using toString() can impact performance. Thanks. Math.floor(Math.log10(number) + 1) // or just (int) Math.log10(number) + 1 For example: int number = 123456; int length = (int) Math.log10(number) +

Print all unique integer partitions given an integer as input

拜拜、爱过 提交于 2019-11-30 04:34:20
I was solving a programming exercise and came across a problem over which I am not able to satisfactorily find a solution. The problem goes as follows: Print all unique integer partitions given an integer as input. Integer partition is a way of writing n as a sum of positive integers. for ex: Input=4 then output should be Output= 1 1 1 1 1 1 2 2 2 1 3 4 How should I think about solving this problem? I was wondering about using recursion. Can anyone provide me an algorithm for this question? Or a hint towards solution. any explanation for such kind of problems is welcome. (I am a beginner in

Getting a integer value from a textbox, how to check if it's NaN or null etc?

大兔子大兔子 提交于 2019-11-30 04:14:15
问题 I am pulling a value via JavaScript from a textbox. If the textbox is empty, it returns NaN . I want to return an empty string if it's null, empty, etc. What check do I do? if(NAN = tb.value) ? 回答1: Hm, something is fishy here. In what browser does an empty textbox return NaN? I've never seen that happen, and I cannot reproduce it. The value of a text box is, in fact a string. An empty text box returns an empty string! Oh, and to check if something is NaN, you should use: if (isNaN(tb.value))

Convert Java Number to BigDecimal : best way

感情迁移 提交于 2019-11-30 04:10:42
I am looking for the best way to convert a Number to a BigDecimal. Is this good enough? Number number; BigDecimal big = new BigDecimal(number.toString()); Can we lose precision with the toString() method ? This is fine, remember that using the constructor of BigDecimal to declare a value can be dangerous when it's not of type String. Consider the below... BigDecimal valDouble = new BigDecimal(0.35); System.out.println(valDouble); This will not print 0.35, it will infact be... 0.34999999999999997779553950749686919152736663818359375 I'd say your solution is probably the safest because of that.

Find first missing number in a sequence of numbers

亡梦爱人 提交于 2019-11-30 04:03:36
问题 I am trying to figure out how to find the first missing number of a sequence of numbers like this (1,2,3,5,6,9,10,15) I want to put the first missing number, #4, into an variable for later use but don't know how to do so? I have tried this but this only gives me the last number: var mynumbers=new Array(1,2,3,6,9,10); for(var i = 1; i < 32; i++) { if(mynumbers[i] - mynumbers[i-1] != 1) { alert("First missing number id: "+mynumbers[i]); break; } } First of all it gives me the first number after