numbers

How to create N-tuples in Python?

心已入冬 提交于 2019-12-22 18:30:26
问题 What would be the easiest way to create a list of n-tuples in Python? For example, if I want to create for a number n (for e.g. 3): I'd want to generate the following set of tuples: (1,1,1) (1,1,2) (1,1,3) (2,1,1) (2,1,2) (2,1,3) (3,1,1) (3,1,2) (3,1,3) (1,2,1) (1,2,2) (1,2,3) (2,2,1) (2,2,2) (2,2,3) (3,2,1) (3,2,2) (3,2,3) (1,3,1) (1,3,2) (1,3,3) (2,3,1) (2,3,2) (2,3,3) (3,3,1) (3,3,2) (3,3,3) 回答1: Use itertools.product: >>> from itertools import product >>> list(product(range(1, 4), repeat

Using javascript replace to replace numbers in a string?

无人久伴 提交于 2019-12-22 17:57:15
问题 I have a string var x='abcdefg1234abcdefg'; I want to replace 1234 with 555 using the x.replace() function like x=x.replace(stuff here) I tried to pass '1234','555' as the parameters but it's not working. Any clues? thanks Edit: The string is in a hidden <input> field. The value of it is: <object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="400" height="385"> <param name="movie" value="player.swf" /> <param name="allowfullscreen" value="true" /> <param

JavaScript Regex for positive numbers with one dot and 2 decimal

风流意气都作罢 提交于 2019-12-22 13:57:45
问题 I am quite new to regex and I have a problem with regex expression. I want to only allow a user to enter positive numbers, with or without one dot and up to 2 decimals (can be only 1 decimal also). Upon user typing the text inside the textbox, and if they type the wrong format, I want to remove the other characters and replace the value with the correct format. Valid examples: 123.12 2 56754 92929292929292.12 0.21 3.1 .90 Invalid examples: 12.1232 2.23332 e666.76 -1.23 -54.3242 3.98A 56B BBB

How to convert a string 3.0103E-7 to 0.00000030103 in Java?

回眸只為那壹抹淺笑 提交于 2019-12-22 13:50:31
问题 How to convert a string 0E-11 to 0.00000000000 in Java? I want to display the number in non scientific notations. I've tried looking at the number formatter in Java, however I need to specific the exact number of decimals I want but I will not always know. I simply want the number of decimal places as specificed by my original number. 回答1: I would use BigDecimal.Pass your string into it as a parameter and then use String.format to represent your newly created BigDecimal without scientific

Random number generator - variable length

旧巷老猫 提交于 2019-12-22 13:47:12
问题 Currently my program (see below) generates random strings (made of numbers) from 8 - 12 characters in length. public static string GenerateNewCode(int CodeLength) { string newCode = String.Empty; int seed = unchecked(DateTime.Now.Ticks.GetHashCode()); Random random = new Random(seed); // keep going until we find a unique code ` do { newCode = random.Next(Convert.ToInt32(Math.Pow(10, CodeLength - 1)), Convert.ToInt32(Math.Pow(10, CodeLength) - 1)).ToString("0000"); } while (!ConsumerCode

Rounding to the Nearest Ending Digits

泄露秘密 提交于 2019-12-22 10:56:35
问题 I have the following function that rounds a number to the nearest number ending with the digits of $nearest , and I was wondering if there is a more elegant way of doing the same. /** * Rounds the number to the nearest digit(s). * * @param int $number * @param int $nearest * @return int */ function roundNearest($number, $nearest, $type = null) { $result = abs(intval($number)); $nearest = abs(intval($nearest)); if ($result <= $nearest) { $result = $nearest; } else { $ceil = $nearest - substr(

Cocoa NSNumberFormatterCurrencyStyle without “$” return zero

你说的曾经没有我的故事 提交于 2019-12-22 10:33:30
问题 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

ATM style decimal places

ぐ巨炮叔叔 提交于 2019-12-22 10:04:37
问题 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? 回答1: 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

How to restrict the EditText input content

删除回忆录丶 提交于 2019-12-22 10:01:11
问题 I am trying to create a simple calculator which provides the EditText for the users to input the numbers. The allowing input content should be [1,2,3,4,5,6,7,8,9,0,.] I know that it is possible to limit the input content by using following code android:digits="1234567890." android:inputType="phone" But how can I prevent the users from adding more than one dot ( . ) into the EditText Box? 回答1: You can use this digits attribute android:digits="0123456789" 回答2: You can use InputFilter limit

How to convert a string with numbers and spaces into an int

元气小坏坏 提交于 2019-12-22 08:53:30
问题 I have a small problem. I am tryng to convert a string like "1 234" to a number:1234 I cant't get there. The string is scraped fro a website. It is possible not to be a space there? Because I've tried methods like str_replace and preg_split for space and nothing. Also (int)$abc takes only the first digit(1). If anyone has an ideea, I'd be greatefull! Thank you! 回答1: This is how I would handle it... <?php $string = "Here! is some text, and numbers 12 345, and symbols !£$%^&"; $new_string =