numbers

Test cases for numeric input

时光毁灭记忆、已成空白 提交于 2019-12-06 02:26:27
问题 What are some common (or worthwhile) tests, test questions, weaknesses, or misunderstandings dealing with numeric inputs? This is a community wiki. Please add to it. For example, here are a couple sample ideas: I commonly see users enter text into number fields (eg, ">4" or "4 days", etc). Fields left blank (null) Very long numeric strings Multiple decimals and commas (eg, "4..4" and "4,,434.4.4") Boundary Value Analysis: Lower Boundary Lower Boundary - 1 (for decimal/float, use smaller

Cocoa NSNumberFormatterCurrencyStyle without “$” return zero

核能气质少年 提交于 2019-12-06 00:44:28
I have a number formatter set up to convert currency strings to decimal values. The problem is that if the text string does not have a leading dollar sign ("$"), it gets converted to 0, rather than a valid matching number. So: "$3.50" converts to 3.50 "3.50" converts to 0 Here is the code for the converter: // formatter to convert a value to and from a currency string NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init]; [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; [currencyFormatter setGeneratesDecimalNumbers:YES]; Am I missing something? I ran into a

Is there a function which does grouping numbers (thousand)?

谁说我不能喝 提交于 2019-12-06 00:09:20
Is there somewhere hidden in a (small) module a function, which does this for me: my $var = 23654325432; $var = reverse $var; $var =~ s/\d{3}\K(?=\d+)/_/g; $var = reverse $var; I like Number::Format but it didn't pass all tests on windows. use Number::Format; my $nf = new Number::Format( -thousands_sep => ',', -decimal_point => '.', ); my $formatted = $nf->format_number( 23142153 ); JRFerguson Look at perl5i . It has commify and group_digits methods. The fastest method is in the perlfaq ( perldoc -q commas ): sub commify { local $_ = shift; 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; return $_; } Of

O(log log n) algorithm for determining if n is perfect square

江枫思渺然 提交于 2019-12-05 23:39:34
Is there any published O(log b) algorithm to determine if a b-bit number is square of an integer? (I apologize if this question is beyond the scope of this site and would happily retrieve it if so) Update: I realize that my question as posed is unreasonable. So let me amend it by asking for any algorithm that is sub polynomial operations in b. Not necessarily O(log^k b) for a constant k, and has "reasonable" space complexity. Operations are defined in the usual sense: something that is reasonable for the task at hand (e.g. add, negate, xor, and, or, etc.) Post script : Now I realize that my

Hint for lookup table set bit count algorithm

落爺英雄遲暮 提交于 2019-12-05 21:25:52
I am looking at solution for the set bit count problem (given a binary number, how to efficiently count how many bits are set). Here, http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive , I have found some methods. What about the lookup table method? I dont understand what properties of binary representation / number make it work. static const unsigned char BitsSetTable256[256] = { # define B2(n) n, n+1, n+1, n+2 # define B4(n) B2(n), B2(n+1), B2(n+1), B2(n+2) # define B6(n) B4(n), B4(n+1), B4(n+1), B4(n+2) B6(0), B6(1), B6(1), B6(2) }; unsigned int v; // count the number of

ATM style decimal places

时光总嘲笑我的痴心妄想 提交于 2019-12-05 20:55:02
I'm working on getting an old piece of code working, from 2003. I'm trying to replicate an ATM style decimal textbox. This code claims to have worked for someone, but I am having trouble implementing it. Maybe someone has a better way of achieving this? Maybe in jQuery? This is how I would solve it: http://jsfiddle.net/77bMx/86/ Handles numpad input as well as standard number keys Works with backspace Will revert to 0.00 if you somehow manage to produce illegal input (like backspacing too much) The basic idea is to intercept any input to the box, make sure it's of the correct type (a number,

What's the best way in JavaScript to test if a given parameter is a square number?

喜欢而已 提交于 2019-12-05 19:53:04
问题 I created a function that will test to see if a given parameter is a square number. Read about square numbers here: https://en.wikipedia.org/?title=Square_number If the number is a square number, it returns true and otherwise false . Negative numbers also return false . Examples: isSquare(-12) // => false isSquare( 5) // => false isSquare( 9) // => true isSquare(25) // => true isSquare(27) // => false Right now, I am using this method: http://jsfiddle.net/marcusdei/ujtc82dq/5/ But, is there a

Reversing a Number using bitwise shift

China☆狼群 提交于 2019-12-05 19:17:49
I am trying to find a way to reverse a number without Converting it to a string to find the length Reversing the string and parsing it back Running a separate loop to compute the Length i am currently doing it this way public static int getReverse(int num){ int revnum =0; for( int i = Integer.toString(num).length() - 1 ; num>0 ; i-- ){ revnum += num % 10 * Math.pow( 10 , i ); num /= 10; } return revnum; } But I would Like to implement the above 3 conditions. I am looking for a way , possibly using the bit wise shift operators or some other kind of bitwise operation. Is it possible ? If so how

Convenience methods for Ruby's Number class and 0

丶灬走出姿态 提交于 2019-12-05 18:05:42
I'm writing convenience methods to check if number is positive or negative like so: class Numeric def positive? self > 0 end def negative? self < 0 end end but in this case I do not know how to handle cases like these: >> 0.positive? >> 0.negative? Update : I've updated the typo in the class name. I used numeric because I needed to check the floats as well. If the problem is that you're getting false for both, either you consider 0 to be positive or not. If so, you should have something like: def positive? self >= 0 end If not, leave it as it is, since 0 is neither positive not negative and

Scheme language: merge two numbers

一世执手 提交于 2019-12-05 18:01:05
How can I merge two integers from a list into one? (in Scheme) Example: '(11 223) -> 11223 Assuming that the list has exactly two elements, and that both are numbers: (define (merge-numbers lst) (let ((1st (number->string (first lst))) (2nd (number->string (second lst)))) (string->number (string-append 1st 2nd)))) It works as expected: (merge-numbers '(11 223)) > 11223 Alternatively, without using a let : (define (merge-numbers lst) (string->number (string-append (number->string (first lst)) (number->string (second lst))))) This is my original answer from Jan 25 '12 at 3:05. It only handles