decimal

What is the largest number the Decimal class can handle?

老子叫甜甜 提交于 2020-01-11 04:42:09
问题 My program calculates the mathematical constant e , which is irrational. In order to do this, I needed to get factorials of very large numbers. int cannot handle numbers larger than 170!. (I found that the largest Google's calculator can handle is 170.654259, but I'm not sure how a non integer can be factorized.) float can not handle very large numbers either. I calculated e to 750000 digits, and math.factorial(750000) is a mind-boggling, large number. Yet, Decimal handled it with apparent

Round money to nearest 10 dollars in Javascript

早过忘川 提交于 2020-01-10 17:12:30
问题 How can I round a decimal number in Javascript to the nearest 10? My math is pretty rubbish today, it could be the 2 hour sleep :/ Some sample cases $2823.66 = $2820 $142.11 = $140 $9.49 = $10 I understand I probably need a combination of Math.round/floor but I can't seem to get expected result. Any help/pointers appreciated! M 回答1: Try Math.round(val / 10) * 10; 回答2: Use this function: function roundTen(number) { return Math.round(number/10)*10; } alert(roundTen(2823.66)); 回答3: To round a

Format Python Decimal object to a specified precision

馋奶兔 提交于 2020-01-10 08:53:11
问题 I've spent countless hours researching, reading, testing, and ultimately confused and dismayed at Python's Decimal object's lack of the most fundamental concept: Formatting a Decimal's output to a string. Let's assume we have some strings or Decimal objects with the following values: 0.0008 11.1111 222.2222 3333.3333 1234.5678 The goal is to simply set the Decimal's precision to the second decimal place. Eg, 11.1111 would be formatted as 11.11 , and 1234.5678 as 1234.57 . I envision code

PHP - Force integer conversion to float with three decimals

不想你离开。 提交于 2020-01-10 04:28:05
问题 I am trying to convert a big array of numbers to a specific format which is +/-NNN.NNN. So, if I have these numbers: $numbers1 = array(-1.23, 0.3222, 10, 5.54); I want their final format to be $numbers2 = array(-001.023, +000.322, +010.000, +005.054); I am trying to do it like this: foreach ($numbers1 as $n) { $fnum = abs(number_format((float)$n, 3, '.', '')); if ($fnum>0 && $fnum<10) { $fnum = '00'.$fnum; } else if ($fnum >= 10 && $fnum<100) { $fnum = '0'.$fnum; } if ($n>0) $fnum = '+'.$fnum

How can I make a “working” repeating decimal representation of a rational number?

╄→尐↘猪︶ㄣ 提交于 2020-01-09 19:27:28
问题 I've figured out how to display the repeating part of a repeating decimal using OverBar. repeatingDecimal doesn't actually work as a repeating decimal. I'd like to make a variation of it that looks and behaves like a repeating decimal. Question How could I make a working repeating decimal representation (possibly using Interpretation[] )? Background Please excuse me if I ramble. This is my first question and I wanted to be clear about what I have in mind. The following will "draw" a repeating

How to round decimal value up to nearest 0.05 value?

北城余情 提交于 2020-01-09 04:59:06
问题 Is there any way to round up decimal value to its nearest 0.05 value in .Net? Ex: 7.125 -> 7.15 6.66 -> 6.7 If its now available can anyone provide me the algo? 回答1: How about: Math.Ceiling(myValue * 20) / 20 回答2: Use this: Math.Round(mydecimal / 0.05m, 0) * 0.05m; The same logic can be used in T-SQL: ROUND(@mydecimal / 0.05, 0) * 0.05 I prefer this approach to the selected answer simply because you can directly see the precision used. 回答3: Something like this should work for any step, not

Split an integer into digits to compute an ISBN checksum

你离开我真会死。 提交于 2020-01-08 16:05:59
问题 I'm writing a program which calculates the check digit of an ISBN number. I have to read the user's input (nine digits of an ISBN) into an integer variable, and then multiply the last digit by 2, the second last digit by 3 and so on. How can I "split" the integer into its constituent digits to do this? As this is a basic homework exercise I am not supposed to use a list. 回答1: Just create a string out of it. myinteger = 212345 number_string = str(myinteger) That's enough. Now you can iterate

Split an integer into digits to compute an ISBN checksum

夙愿已清 提交于 2020-01-08 16:04:06
问题 I'm writing a program which calculates the check digit of an ISBN number. I have to read the user's input (nine digits of an ISBN) into an integer variable, and then multiply the last digit by 2, the second last digit by 3 and so on. How can I "split" the integer into its constituent digits to do this? As this is a basic homework exercise I am not supposed to use a list. 回答1: Just create a string out of it. myinteger = 212345 number_string = str(myinteger) That's enough. Now you can iterate

Why in this Java program the Doubles in a String Output have too many decimals?

那年仲夏 提交于 2020-01-07 08:36:08
问题 In a solution to an exercise in the Book Art and Science of Java I had to write a program that converts Kilograms into the corresponding values in Pounds and Ounces. I wrote the program but when I try to convert say 1kg, the result the program gives me is: 1 kg = 2 pounds and 3.200000000000006 ounces Now my constants are 2.2 pounds per kg and 16 ounces per pound so 3.2 ounces is correct. But not with so many 0's and that 6 at the end freaks me out. Anyone know why this happens and how it can

Converting input number to Mysql decimal

﹥>﹥吖頭↗ 提交于 2020-01-07 05:24:09
问题 I'm trying to store decimals taken from input in a Mysql decimal (10,2) row. But when I enter 37.50 in the input, Mysql only stores 37.00 It seems to disregard everything after the dot. How can I store decimals? this the input <input class='form-control formBlock' name='payrate' value='' step="0.01" type='number' placeholder="Pay Rate..." required> So far I tried float: $payrate=floatval($_POST['payrate']); 回答1: It's unclear what you are asking. (Was there a question? Or was all that just