numbers

float is OK, int gives wrong output python 2.7 [duplicate]

妖精的绣舞 提交于 2019-12-02 19:02:25
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Why doesn’t this division work in python? I have this and works fine def roi(stake, profit): your_roi = profit / stake * 100 return your_roi def final_roi(): roi1 = roi(52, 7.5) print "%.2f" % roi1 final_roi() but if I change the profit number to an int (meaning both stake and profit will have an int value) e.g. 52, 7 it is giving the output of 0.00. what's wrong there? I thought it had been formatted to be a

An improved isNumeric() function?

最后都变了- 提交于 2019-12-02 18:47:17
During some projects I have needed to validate some data and be as certain as possible that it is javascript numerical value that can be used in mathematical operations. jQuery, and some other javascript libraries already include such a function, usually called isNumeric. There is also a post on stackoverflow that has been widely accepted as the answer, the same general routine that the fore mentioned libraries are using. function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n); } Being my first post, I was unable to reply in that thread. The issue that I had with the accepted post

String numbers into number numbers in PHP

∥☆過路亽.° 提交于 2019-12-02 18:36:19
问题 How come when I set 00 as a value like this: $var = 00; it ouputs as 0 when I use it? How can I set 00 so that $var++ would become 01? 回答1: Numbers in PHP (and virtually all languages) are not stored internally with leading (or in the case of decimal values, trailing) zeros. There are many ways to display your numeric variables with leading zeros In PHP. The simplest way is to convert your value to a string, and pad the string with zero's until it's the correct length. PHP has a function

How can I generate a specific set of numbers using a pseudorandom number generator? [closed]

与世无争的帅哥 提交于 2019-12-02 18:14:55
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I am frustrated because I do not know if what I am attempting is even possible. Lets say I have a list of unsigned numbers. I want to create a seed for a certain pseudorandom number generator algorithm that can generate that specific set of numbers. When creating the seed, I know the length of the

Standalone numbers Regex?

纵饮孤独 提交于 2019-12-02 18:06:35
问题 I currently use this regex: (\d+) the problem that i can get 2 strings: "2112343 and alot of 4.99" OR "4.99 and alot of 2112343 " I get this from both: [2112343, 4, 99] I need to get only the 2112343 ... How can i achieve this? 回答1: Using lookaround, you can restrict your capturing to only digits which are not surrounded by other digits or decimal points: (?<![0-9.])(\d+)(?![0-9.]) Alternatively, if you want to only match stand-alone numbers (e.g. if you don't want to match the 123 in

Return row number(s) for a particular value in a column in a dataframe

你。 提交于 2019-12-02 17:56:10
I have a data frame (df) and I was wondering how to return the row number(s) for a particular value (2585) in the 4th column (height_chad1) of the same data frame? I've tried: row(mydata_2$height_chad1, 2585) and I get the following error: Error in factor(.Internal(row(dim(x))), labels = labs) : a matrix-like object is required as argument to 'row' Is there an equivalent line of code that works for data frames instead of matrix-like objects? Any help would be appreciated. SethB Use which(mydata_2$height_chad1 == 2585) Short example df <- data.frame(x = c(1,1,2,3,4,5,6,3), y = c(5,4,6,7,8,3,2,4

How to Generate a random number of fixed length using JavaScript?

杀马特。学长 韩版系。学妹 提交于 2019-12-02 17:51:27
I'm trying to generate a random number that must have a fixed length of exactly 6 digits. I don't know if JavaScript has given below would ever create a number less than 6 digits? Math.floor((Math.random()*1000000)+1); I found this question and answer on StackOverflow here . But, it's unclear. EDIT: I ran the above code a bunch of times, and Yes, it frequently creates numbers less than 6 digits. Is there a quick/fast way to make sure it's always exactly 6 digits? Cilan console.log( Math.floor(100000 + Math.random() * 900000) ); Will always create a number of 6 digits and it ensures the first

javascript onclick increment number

ε祈祈猫儿з 提交于 2019-12-02 17:47:24
With javascript, how can I do it so when i click a form button it adds 1 to a number? The number it increments could be in a form text field or something. Obviously it'd be on onclick but I'm not sure of the code. Since you gave me nothing to start on, here is a simple example. jsFiddle Example implementation: function incrementValue() { var value = parseInt(document.getElementById('number').value, 10); value = isNaN(value) ? 0 : value; value++; document.getElementById('number').value = value; } Example Html <form> <input type="text" id="number" value="0"/> <input type="button" onclick=

Fastest way to separate the digits of an int into an array in .NET?

混江龙づ霸主 提交于 2019-12-02 17:39:15
I want to separate the digits of an integer, say 12345, into an array of bytes {1,2,3,4,5}, but I want the most performance effective way to do that, because my program does that millions of times. Any suggestions? Thanks. Jon Skeet How about: public static int[] ConvertToArrayOfDigits(int value) { int size = DetermineDigitCount(value); int[] digits = new int[size]; for (int index = size - 1; index >= 0; index--) { digits[index] = value % 10; value = value / 10; } return digits; } private static int DetermineDigitCount(int x) { // This bit could be optimised with a binary search return x < 10

Determine if a String is a number and convert in Java?

怎甘沉沦 提交于 2019-12-02 17:33:58
I know variants of this question have been asked frequently before (see here and here for instance), but this is not an exact duplicate of those. I would like to check if a String is a number, and if so I would like to store it as a double . There are several ways to do this, but all of them seem inappropriate for my purposes. One solution would be to use Double.parseDouble(s) or similarly new BigDecimal(s) . However, those solutions don't work if there are commas present (so "1,234" would cause an exception). I could of course strip out all commas before using these techniques, but that would