decimal

Convert binary vector to decimal

久未见 提交于 2019-12-07 02:07:30
问题 I have a vector of a binary string: a<-c(0,0,0,1,0,1) I would like to convert this vector into decimal. I tried using the compositions package and the unbinary() function, however, this solution and also most others that I have found on this site require g-adic string as input argument. My question is how can I convert a vector rather than a string to decimal? to illustrate the problem: library(compositions) unbinary("000101") [1] 5 This gives the correct solution, but: unbinary(a) unbinary(

Parse string to decimal, commas and periods

本小妞迷上赌 提交于 2019-12-07 02:07:22
问题 How to parse string to decimal so it would work for both formats - w/ commas and periods? [Fact] public void foo(){ var a="1,1"; var b="1.1"; Assert.Equal(Parse(a),Parse(b)); } private decimal Parse(string s){ return decimal.Parse(s,NumberStyles.Any, CultureInfo.InvariantCulture); } output: Test 'Unit.Sandbox.foo' failed: Assert.Equal() Failure Expected: 11 Actual: 1,1 回答1: You could try that: private decimal Parse(string s){ s = s.Replace(",", CultureInfo.InvariantCulture.NumberFormat

Python decimal.Decimal id not the same

两盒软妹~` 提交于 2019-12-06 20:44:26
I am having a problem with encoding to JSON in Python, specifically with decimal.Decimal values. I am using this to output JSON for a Google App Engine application. To circumvent the exception from the default json module in Python telling me that it can't handle decimal.Decimal objects, I am using this encoder subclass: class DecimalEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, decimal.Decimal): return float(o) return super(DecimalEncoder, self).default(o) On other applications, this does work. In this case it doesn't. After much frustration I found out that this gives

Rounding to the nearest hundredth of a decimal in JavaScript

不羁岁月 提交于 2019-12-06 20:43:19
问题 I'm doing simple math in JavaScript using variables to represent the numbers. Here is an example of my code: var ones = 0; var fives = 0; function results (){ _fives = (fives * 5); var res = (_fives + ones); document.innerHTML = res; } This isn't the full code but basically I'm having the user enter the amount of bills and coins from 1 cent coins up to $100 bills. The code multiplies the amount of bills to the amount the bill is worth. This is no problem works just fine... For some reason on

Convert decimal to percent or shift decimal places. How

天涯浪子 提交于 2019-12-06 20:06:07
问题 I have a that is generated from the diference of 2 numbers, but it return for exemple 0,07 for 7% and 0,5 for 50% i just want to fix to reach these goar, like 15,2% 13% and so on. How can I do that? do c# has something embeded on CLR to do that? 回答1: You can use the Percentage Custom Numeric Format String. This will do it without you having to multiply yourself. For example, you can do: double value = 0.152; string result = value.ToString("#0.##%"); If your locale is set to European (my guess

Django object filter - price behaving strangely, eg 170 treated as 17 etc

[亡魂溺海] 提交于 2019-12-06 14:50:16
问题 I have a simple object filter that uses price__lt and price__gt . This works on a property on my product model called price, which is a CharField [string] (decimal saw the same errors, and caused trouble with aggregation so reverted to string). It seems that when passing in these values to the filter, they are treated in a strange way, eg 10 is treated as 100. for example: /products/price/10-200/ returns products priced 100-200. the filters are being passed in as filterargs: FILTER ARGS: {

C Fast base convert from decimal to ternary

一个人想着一个人 提交于 2019-12-06 14:19:57
is there any method of changing decimal number to ternary ? I mean i don't want to use modulo and divide method, i have very big decimal number, something like 128123832812381835828638486384863486.............1237127317237 and so on. Also don't want to use bigints. Is there any method ? You don't have to use divide/modulo. Instead, iterate over the input digits, from low to high. For each digit position, first calculate what 1000....000 would be in the output representation (it's 10x the previous power of 10). Then multiply that result by the digit, and accumulate into the output

How do I convert a decimal fraction to binary in Java?

霸气de小男生 提交于 2019-12-06 13:59:40
问题 I need to convert 0.5 in base 10 to base 2 (0.1). I have tried using Double.doubleToRawLongBits(0.5) and it returns 4602678819172646912 which I guess is in hex, but it does not make sense to me. 回答1: Multiply you number by 2^n, convert to an BigInteger, convert to binary String, add a decimal point at position n (from right to left). Example (quick & ++dirty): private static String convert(double number) { int n = 10; // constant? BigDecimal bd = new BigDecimal(number); BigDecimal mult = new

Convert a large 2^63 decimal to binary

独自空忆成欢 提交于 2019-12-06 13:21:49
I need to convert a large decimal to binary how would I go about doing this? Decimal in question is this 3324679375210329505 http://www.wikihow.com/Convert-from-Decimal-to-Binary How about: String binary = Long.toString(3324679375210329505L, 2); You may want to go for BigDecimal . A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale.The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal. new BigDecimal

Validating decimal in C# for storage in SQL Server

女生的网名这么多〃 提交于 2019-12-06 11:04:41
问题 I have a decimal database column decimal (26,6) . As far as I can gather this means a precision of 26 and a scale of 6. I think this means that the number can be a total of 26 digits in length and 6 of these digits can be after the decimal place. In my WPF / C# frontend I need to validate an incoming decimal so that I can be sure that it can be stored in SQL Server without truncation etc. So my question is there a way to check that decimal has a particular precision and scale. Also as an