numbers

Difference between 2 numbers

霸气de小男生 提交于 2019-11-28 09:34:49
I need the perfect algorithm or C# function to calculate the difference (distance) between 2 decimal numbers. For example the difference between: 100 and 25 is 75 100 and -25 is 125 -100 and -115 is 15 -500 and 100 is 600 Is there a C# function or a very elegant algorithm to calculate this or I have to go and handle every case separately with if s. If there is such a function or algorithm, which one is it? You can do it like this public decimal FindDifference(decimal nr1, decimal nr2) { return Math.Abs(nr1 - nr2); } result = Math.Abs(value1 - value2); Just adding this, as nobody wrote it here:

Javascript: Random number out of 5, no repeat until all have been used

為{幸葍}努か 提交于 2019-11-28 09:31:30
I am using the below code to assign a random class (out of five) to each individual image on my page. $(this).addClass('color-' + (Math.floor(Math.random() * 5) + 1)); It's working great but I want to make it so that there are never two of the same class in a row. Even better would be if there were never two of the same in a row, and it also did not use any class more than once until all 5 had been used... As in, remove each used class from the array until all of them have been used, then start again, not allowing the last of the previous 5 and the first of the next 5 to be the same color.

Add numbers in c#

拥有回忆 提交于 2019-11-28 09:24:23
问题 i have a numerical textbox which I need to add it's value to another number I have tried this code String add = (mytextbox.Text + 2) but it add the number two as another character like if the value of my text box is 13 the result will become 132 回答1: String add = (Convert.ToInt32(mytextbox.Text) + 2).ToString(); You need to convert the text to an integer to do the calculation. 回答2: The type of mytextbox.Text is string . You need to parse it as a number in order to perform integer arithmetic,

Magento - Removing numbers in url key/product url

拜拜、爱过 提交于 2019-11-28 09:14:24
As you may know, if you have products that share a url key, the url key will have a digit appended to it: i.e http://www.example.com/main-category/sub-category/product-name-**6260**.html How do I find the source of that 6260 (which is one of the #'s appended to my urls)? I tried product id, sku, I cannot find the source of it. The reason I ask is because if I can find it, I can create a string replace function to flush it out of url's before I echo them on certain product listing pages. Thanks. Before we get to the location in code where this happens, be advised you're entering a world of pain

Using Ruby convert numbers to words?

冷暖自知 提交于 2019-11-28 09:13:38
How to convert numbers to words in ruby? I know there is a gem somewhere. Trying to implement it without a gem. I just need the numbers to words in English for integers. Found this but it is very messy. If you have any idea on how to implement a cleaner easier to read solution please share. http://raveendran.wordpress.com/2009/05/29/ruby-convert-number-to-english-word/ Here is what I have been working on. But having some problem implementing the scales. The code is still a mess. I hope to make it more readable when it functions properly. class Numberswords def in_words(n) words_hash = {0=>

C# find biggest number

亡梦爱人 提交于 2019-11-28 09:13:23
It's the first time I am using c# so I am not very familiar with it. I would like to create a simple program to find the biggest number if I have the user entering 3 numbers. I just need to know what to put in the code, because I am not very sure. Use Math.Max : int x = 3, y = 4, z = 5; Console.WriteLine(Math.Max(Math.Max(x, y), z)); There is the Linq Max() extension method. It's available for all common number types(int, double, ...). And since it works on any class that implements IEnumerable<T> it works on all common containers such as arrays T[] , List<T> ,... To use it you need to have

C++ - how to find the length of an integer

血红的双手。 提交于 2019-11-28 09:09:16
I'm trying to find a way to find the length of an integer (number of digits) and then place it in an integer array. The assignment also calls for doing this without the use of classes from the STL, although the program spec does say we can use "common C libraries" (gonna ask my professor if I can use cmath, because I'm assuming log10(num) + 1 is the easiest way, but I was wondering if there was another way). Ah, and this doesn't have to handle negative numbers. Solely non-negative numbers. I'm attempting to create a variant "MyInt" class that can handle a wider range of values using a dynamic

JavaScript type conversion: (true && 1) vs (true | | 1)

吃可爱长大的小学妹 提交于 2019-11-28 09:07:03
JavaScript is non-strictly typed language as Java,for example. As we know, it converts value of result dependently upon context: "2" + "3" results "23" "2" * "3" results 6 This is quite clear and OK for understanding. I just tried following expressions and got confused: true && 1 results 1 true || 1 results true Why the first gives Number and the second gives boolean? Considering JavaScript conversion rules,I expect to get boolean values in both cases,due to boolean context of expression. Check Douglas Crockford's site , it says: The && operator is commonly called logical and. It can also be

Checking a number range with regular expressions

北城余情 提交于 2019-11-28 09:05:46
I'm using a regular expression to validate a certain format in a string. This string will become a rule for a game. Example: "DX 3" is OK according to the rule, but "DX 14" could be OK too... I know how to look at the string and find one or more "numbers", so the problem is that the regex will match 34 too, and this number is out of "range" for the rule... Am I missing something about the regex to do this? Or is this not possible at all? Unfortunately there's no easy way to define ranges in regex. If you are to use the range 1-23 you'll end up with a regex like this: ([1-9]|1[0-9]|2[0-3])

stripping out all characters from a string, leaving numbers

ε祈祈猫儿з 提交于 2019-11-28 09:03:03
Hay, i have a string like this: v8gn5.8gnr4nggb58gng.g95h58g.n48fn49t.t8t8t57 I want to strip out all the characters leaving just numbers (and .s) Any ideas how to do this? Is there a function prebuilt? thanks $str = preg_replace('/[^0-9.]+/', '', $str); replace substrings that do not consist of digits or . with nothing. preg_replace('/[^0-9.]/', '', $string); Juraj Blahunka $input = 'some str1ng 234'; $newString = preg_replace("/[^0-9.]/", '', $input); Veger To satisfy my curiosity I asked about the speed of the proposed answers and as shown in preg_replace speed optimisation/ it is (much)