numbers

PHP show only significant (non-zero) decimals

无人久伴 提交于 2019-11-30 22:24:02
问题 In PHP (using built-in functions) I'd like to convert/format a number with decimal, so that only the non-zero decimals show. However, another requirement of mine is that if it's a number without a decimal value, I'd still like to show that zero. Examples: 9.000 -> 9.0 9.100 -> 9.1 9.120 -> 9.12 9.123 -> 9.123 rtrim($value, "0") almost works. The problem with rtrim is that it leaves 9.000 as 9. . sprintf() seemed like a candidate, but I couldn't get it to have a variable amount of decimals.

When I divide numbers in clojure I get a fraction , how do I get the decimal?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 22:17:46
问题 When I do (/ 411 125) , I don't get it in terms of decimal. How do I do that? 回答1: user> (float (/ 411 125)) 3.288 user> (double (/ 411 125)) 3.288 回答2: user=> (clojure-version) "1.4.0" user=> (doc quot) ------------------------- clojure.core/quot ([num div]) quot[ient] of dividing numerator by denominator. nil user=> (quot 411 125) 3 回答3: As documented, integer division yields rational numbers. Try (/ 411.0 125) 回答4: If you use a float for the dividend, you'll get a decimal answer. (/ 22.0 7

Round to .5 or 1.0 in SQL

守給你的承諾、 提交于 2019-11-30 21:45:12
问题 I'm looking to round values like 2.3913 -> 2.5 4.6667 -> 4.5 2.11 -> 2 How can I manage this in SQL? Thanks 回答1: SELECT ROUND(2.2 * 2, 0) / 2 gets you to the nearest .5 来源: https://stackoverflow.com/questions/9873990/round-to-5-or-1-0-in-sql

How to get numbers out of string?

空扰寡人 提交于 2019-11-30 21:41:03
I'm using a Java StreamTokenizer to extract the various words and numbers of a String but have run into a problem where numbers which include commas are concerned, e.g. 10,567 is being read as 10.0 and ,567. I also need to remove all non-numeric characters from numbers where they might occur, e.g. $678.00 should be 678.00 or -87 should be 87. I believe these can be achieved via the whiteSpace and wordChars methods but does anyone have any idea how to do it? The basic streamTokenizer code at present is: BufferedReader br = new BufferedReader(new StringReader(text)); StreamTokenizer st = new

Non-Uniform Random Number Generator Implementation?

两盒软妹~` 提交于 2019-11-30 21:33:32
I need a random number generator that picks numbers over a specified range with a programmable mean. For example, I need to pick numbers between 2 and 14 and I need the average of the random numbers to be 5. I use random number generators a lot. Usually I just need a uniform distribution. I don't even know what to call this type of distribution. Thank you for any assistance or insight you can provide. You might be able to use a binomial distribution , if you're happy with the shape of that distribution. Set n=12 and p=0.25. This will give you a value between 0 and 12 with a mean of 3. Just add

Generate unique number based on string input in Javascript

久未见 提交于 2019-11-30 21:31:11
In the past I have made a function that generates an unique id (number) from a string. Today I discover that it is not as unique as should be. Never saw a problem before with it. Today two different inputs generates the same id (number). I use the same technique in Delphi, C++, PHP and Javascript to generate the same id's so there is no difference when different languages are involved to a project. For example this can be handy to communicate, for HTML id's, tempfiles etc. In general, what I do is calculate a CRC16 of a string, add the sum and return it. For example, these two strings generate

In Bash, how to convert number list into ranges of numbers?

点点圈 提交于 2019-11-30 20:34:19
Currently I have a sorted output of numbers from a command: 18,19,62,161,162,163,165 I would like to condense these number lists into a list of single numbers or ranges of numbers 18-19,62,161-163,165 I thought about trying to sort through the array in bash and read the next number to see if it is +1... I have a PHP function that does essentially the same thing, but I'm having trouble transposing it to Bash: foreach ($missing as $key => $tag) { $next = $missing[$key+1]; if (!isset($first)) { $first = $tag; } if($next != $tag + 1) { if($first == $tag) { echo '<tr><td>'.$tag.'</td></tr>'; } else

Defining cross-platform money_format function (Linux and Windows)

﹥>﹥吖頭↗ 提交于 2019-11-30 20:24:00
I have read that money_format is not available on windows, and on some Linux distributions (i.e. BSD 4.11 variants). But I want to write cross-platform library using normal function, when available and using this workaround when not, so my library will be able to run on every PHP-based web server. Is there any simple solution to check whether built-in function is available and if not to include the solution from above? The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows. So you can use this

Find first missing number in a sequence of numbers

柔情痞子 提交于 2019-11-30 20:17:07
I am trying to figure out how to find the first missing number of a sequence of numbers like this (1,2,3,5,6,9,10,15) I want to put the first missing number, #4, into an variable for later use but don't know how to do so? I have tried this but this only gives me the last number: var mynumbers=new Array(1,2,3,6,9,10); for(var i = 1; i < 32; i++) { if(mynumbers[i] - mynumbers[i-1] != 1) { alert("First missing number id: "+mynumbers[i]); break; } } First of all it gives me the first number after an "hole" in the numbersequence, secondly it continues to alert all numbers comming after an "hole" if

Fortran: handling integer values of size: ~700000000000

爱⌒轻易说出口 提交于 2019-11-30 20:02:39
Currently I'm brushing up on my Fortran95 knowledge (don't ask why)... I'm running in to a problem though. How does one handle large integers, eg. the size of: ~700000000000 INTEGER(KIND=3) cannot hold this number. If anyone is interested the compiler I have available is Silverfrost FTN95. I am using the integer to run through a larger set of data. Do you have any suggestions? The standard solution (since Fortran 95, so I assume your compiler supports it) is to use the SELECTED_INT_KIND intrinsic to probe for valid integer kinds (whose values are compiler dependent) and the HUGE intrinsic.