numbers

Format a number as 2.5K if a thousand or more, otherwise 900

故事扮演 提交于 2019-12-17 02:55:08
问题 I need to show a currency value in the format of 1K of equal to one thousand, or 1.1K, 1.2K, 1.9K etc, if its not an even thousands, otherwise if under a thousand, display normal 500, 100, 250 etc, using javascript to format the number? 回答1: Sounds like this should work for you: function kFormatter(num) { return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num) } console.log(kFormatter(1200)); // 1.2k console.log(kFormatter(-1200)); //

Generating the partitions of a number

社会主义新天地 提交于 2019-12-17 02:44:26
问题 I needed an algorithm to generate all possible partitions of a positive number, and I came up with one (posted as an answer), but it's exponential time. The algorithm should return all the possible ways a number can be expressed as the sum of positive numbers less than or equal to itself. So for example for the number 5 , the result would be: 5 4+1 3+2 3+1+1 2+2+1 2+1+1+1 1+1+1+1+1 So my question is: is there a more efficient algorithm for this? EDIT: Question was titled "Sum decomposition of

Java Generate Random Number Between Two Given Values [duplicate]

孤街醉人 提交于 2019-12-17 02:42:08
问题 This question already has answers here : How do I generate random integers within a specific range in Java? (66 answers) Closed 5 years ago . I would like to know how to generate a random number between two given values. I am able to generate a random number with the following: Random r = new Random(); for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[i].length; j++){ a[i][j] = r.nextInt(); } } However, how can I generate a random number between 0 and 100 (inclusive)? 回答1: You could use

What is special about numbers starting with zero?

谁都会走 提交于 2019-12-17 02:24:57
问题 This is kinda stupid question, but it's interesting for me ) This is what i get with visual studio 2013 int i = 07; // i == 7 int i = 16; // i == 16 int i = 00016; // i == 14, why? int i = 05016; // i == 2574, wow ) int i = 08; // compile error, compiler expects octal number... If number starts with zero and contains 8, it's compile error. Is this normal? And what exactly compiler does with starting zeros if 00016 == 14? Thanks to all )) 回答1: Yes, this is expected. [C++11: 2.14.2/1]: An

What does it mean when a numeric constant in C/C++ is prefixed with a 0?

落爺英雄遲暮 提交于 2019-12-17 02:24:34
问题 Ok... So I had a silly idea and tried putting the value 0123 into an int, just curious to see what would happen, I assumed that when I printed the value I'd get 123, but instead I got 83... Any ideas why? what happens inside the compiler/memory that makes this value become 83? I tried this in C++ and C with GCC compiler and also tried with a float which yielded the same results. 回答1: In C/C++ a numeric literal prefixed with a '0' is octal (base 8). See http://www.cplusplus.com/doc/tutorial

Converting words to numbers in PHP

守給你的承諾、 提交于 2019-12-17 02:14:09
问题 I am trying to convert numerical values written as words into integers. For example, "iPhone has two hundred and thirty thousand seven hundred and eighty three apps" would become "iPhone as 230783 apps" Before i start coding, I would like to know if any function / code exists for this conversion. 回答1: There are lots of pages discussing the conversion from numbers to words. Not so many for the reverse direction. The best I could find was some pseudo-code on Ask Yahoo. See http://answers.yahoo

How to make HTML input tag only accept numerical values?

纵然是瞬间 提交于 2019-12-17 02:09:11
问题 I need to make sure that a certain <input> field only takes numbers as value. The input is not part of a form. Hence it doesn't get submitted, so validating during submission is not an option. I want the user to be unable to type in any characters other than numbers. Is there a neat way to achieve this? 回答1: HTML 5 You can use HTML5 input type number to restrict only number entries: <input type="number" name="someid" /> This will work only in HTML5 complaint browser. Make sure your html

把数组排成最小的数

一世执手 提交于 2019-12-16 22:28:51
输入一个正整数 数组 ,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。 解题: 将各个元素从小到大进行排序,排序之后再把他们串联起来就可以了,全排列时间复杂度为O(n!)。要定义一个比较大小的函数,对拼接后的字符串进行比较。比较两个字符串s1, s2大小的时候,先将它们拼接起来,比较s1+s2,和s2+s1那个大,如果s1+s2大,那说明s2应该放前面。想到了贪心的思想,既然整个序列是最小的,那么越靠前的序列肯定也是最小的,任何两个序列的组合也是较小的。同时将两个字符串按不同顺序相加得到的长度总是相等的,此时就可以简单地使用 compareTo 的方法来做比较。 Comparator接口,两个对象要使用compareTo方法比较大小,就必须实现Comparator接口的compare方法,比如String就实现了这个方法,所以可以直接使用compareTo进行比较。sort(List,Comparator):根据指定的 Comparator 产生的顺序对 List 集合元素进行排序。 public String PrintMinNumber(int [] numbers) { ArrayList<String> arrayList = new ArrayList

Why is 08 not a valid integer literal in Java?

↘锁芯ラ 提交于 2019-12-16 19:57:10
问题 Why is 08 considered an out of range int but 07 and below are not? 回答1: In Java and several other languages, an integer literal beginning with 0 is interpreted as an octal (base 8) quantity. For single-digit numbers (other than 08 and 09 , which are not allowed), the result is the same, so you might not notice that they are being interpreted as octal. However, if you write numbers with more than one significant digit you might be confused by the result. For example: 010 == 8 024 == 20 Since

Generate unique random numbers between 1 and 100

人盡茶涼 提交于 2019-12-16 19:27:07
问题 How can I generate some unique random numbers between 1 and 100 using JavaScript? 回答1: For example: To generate 8 unique random numbers and store them to an array, you can simply do this: var arr = []; while(arr.length < 8){ var r = Math.floor(Math.random() * 100) + 1; if(arr.indexOf(r) === -1) arr.push(r); } console.log(arr); 回答2: Populate an array with the numbers 1 through 100. Shuffle it. Take the first 8 elements of the resulting array. 回答3: Generate permutation of 100 numbers and then