numbers

Python - Ensuring a variable holds a positive number

此生再无相见时 提交于 2019-12-04 14:00:08
问题 I am looking for an elegant way to ensure that a given variable remains positive. I have two variables that hold positive float numbers and I decrement them according to certain conditions. At the end I want to guarantee that I still have positive numbers (or 0 at most). The pseudo code looks something like this: list = [...] value1 = N value2 = M for element in list: if ... : value1 -= X if ... : value2 -= Y Is there a more elegant solution than just adding two ifs at the end? 回答1: I am

Is there an easy way to convert a number to Indian words format with lakhs and crores?

时光怂恿深爱的人放手 提交于 2019-12-04 13:48:14
As the title suggests I would like to convert a long number to the format with words using C#. The Culture settings don't seem to do this and I am just currently doing this String.Format(new CultureInfo("en-IN"), "{0:C0}", Price) But for very long numbers I would prefer the word format. I am not from India and only vaguely familiar with how the system works. While I can't give you the code itself, here's the system 1 - One 10 - Ten 1000 - Thousand 10000 - Ten Thousand 100000 - Lakh 1000000 - Ten Lakh 10000000 - Crore 100000000 - Ten Crore 1000000000 - Arab 10000000000 - Ten Arab 100000000000 -

PHP Random Number

南笙酒味 提交于 2019-12-04 13:13:31
I want to generate a random number in PHP where the digits itself should not repeat in that number. Is that possible? Can you paste sample code here? Ex: 674930, 145289. [i.e Same digit shouldn't come] Thanks Here is a good way of doing it: $amountOfDigits = 6; $numbers = range(0,9); shuffle($numbers); for($i = 0;$i < $amountOfDigits;$i++) $digits .= $numbers[$i]; echo $digits; //prints 217356 If you wanted it in a neat function you could create something like this: function randomDigits($length){ $numbers = range(0,9); shuffle($numbers); for($i = 0;$i < $length;$i++) $digits .= $numbers[$i];

1: command not found

爱⌒轻易说出口 提交于 2019-12-04 13:10:26
I'm writing a divides-by-three function in Bash, and it won't let me set a variable to a number. fizzy.sh: #!/usr/bin/env sh div3() { return `$1 % 3 -eq 0` } d=div3 1 echo $d Example: $ ./fizzy.sh ./fizzy.sh: line 7: 1: command not found Bash functions normally "return" values by printing them to standard output, where the caller can capture them using `func args ...` or $(func args ...) This makes functions work like external commands. The return statement, on the other hand, sets the value of $? . Normally that's going to be set to 0 for success, 1 for failure, or some other value for a

Java: how to output double-type number to integer format if its decimal is 0

五迷三道 提交于 2019-12-04 13:06:07
I have an array of double-type numbers {1.0, 2.3, 3.45, 7.8, 9.0, 5.0}. When outputting the numbers, I want to have those with decimal of 0 to show up like an integer: for example, 1.0 => 1, 9.0 => 9. In java, how do you do this easily? thanks Not using any libraries I'd suggest: public static String parse(double num) { if((int) num == num) return Integer.toString((int) num); //for you, StackOverflowException return String.valueOf(num); //and for you, Christian Kuetbach } Casting a double to int will cut off decimal places. Feel free to convert the number to a string in whatever way you please

How to Add values in a table together using jQuery?

故事扮演 提交于 2019-12-04 13:03:48
I have an HTML table with number values. What I need to do is to get all these values and add them together using jQuery. Any ideas? Example table: http://pastie.org/998759 You can call .each : var sum = 0; $('table td').each(function() { sum += parseFloat($(this).text()); }); I'm going to give you some pseudo-code of how I would do it (mainly because I can't be stuffed writing the whole thing). Create an object/array/variables to store the totals. jQuery select the table Use .each() to loop through each tr . Loop each td and add the value to the relevant total. Once all of the tr have been

Keep reading numbers until newline reached using a Scanner

吃可爱长大的小学妹 提交于 2019-12-04 12:31:53
问题 I want to read a couple of numbers from the console. The way I wanna do this is by having the user enter a sequence of numbers separated by a space. Code to do the following: Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int i = sc.nextInt(); //... do stuff with i ... } The problem is, how do I stop when reaching a new line (while keeping the easy-to-read code above)? Adding a !hasNextLine() to the argument above makes it quit the loop instantly. One solution would be reading

How to Convert from a Residual Number System to a Mixed Radix System?

江枫思渺然 提交于 2019-12-04 11:50:34
I understand the concept of a Residual Number System and the concept of a Mixed Radix system , but I'm having difficulty getting any of the conversion methods I find to work in a simple case study. I started at Knuth's Art of Computer Programming but that had a bit too much on the theory of the conversion, and once Euler was mentioned I was lost. Wikipedia has a nice section on the subject, which I tried here and here but both times I couldn't get back to the number where I started. I found a good article here (PDF) , which I condensed the relevant sections here , but I don't understand the

想要提升Python代码效率?这五个高级方法是必须要会的

半城伤御伤魂 提交于 2019-12-04 11:19:38
前言 文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。 作者: 机器之心 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef 下面是 Python 的 5 种高级特征,以及它们的用法。 Lambda 函数 Lambda 函数是一种比较小的匿名函数——匿名是指它实际上没有函数名。 Python 函数通常使用 def a_function_name() 样式来定义,但对于 lambda 函数,我们根本没为它命名。这是因为 lambda 函数的功能是执行某种简单的表达式或运算,而无需完全定义函数。 lambda 函数可以使用任意数量的参数,但表达式只能有一个。 1 x = lambda a, b : a * b 2 print(x(5, 6)) # prints 30 3 ​ 4 x = lambda a : a*3 + 3 5 print(x(3)) # prints 12 6 ​ 看它多么简单!我们执行了一些简单的数学运算,而无需定义整个函数。这是 Python 的众多特征之一,这些特征使它成为一种干净、简单的编程语言。 Map 函数 Map() 是一种内置的

Swift number formatting

元气小坏坏 提交于 2019-12-04 10:51:03
问题 I am just starting to get to know Swift but I am having a serious problem with number formatting at an extremely basic level. For example, I need to display an integer with at least 2 digits (e.g. 00, 01, 02, 03, 04, 05 ...). The normal syntax I'd expect would be something like: println(" %02i %02i %02i", var1, var2, var3); ...but I don't find any clear instruction for how to achieve this in Swift. I find it really hard to believe that I need to create a custom function to do that. The same