decimal

How can I Convert a Big decimal number to Hex in C# (Eg : 588063595292424954445828)

爷,独闯天下 提交于 2019-12-04 19:50:33
The number is bigger than int & long but can be accomodated in Decimal . But the normal ToString or Convert methods don't work on Decimal . I believe this will produce the right results where it returns anything, but may reject valid integers. I dare say that can be worked around with a bit of effort though... (Oh, and it will also fail for negative numbers at the moment.) static string ConvertToHex(decimal d) { int[] bits = decimal.GetBits(d); if (bits[3] != 0) // Sign and exponent { throw new ArgumentException(); } return string.Format("{0:x8}{1:x8}{2:x8}", (uint)bits[2], (uint)bits[1],

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

社会主义新天地 提交于 2019-12-04 19:39:21
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. 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 BigDecimal(2).pow(n); bd = bd.multiply(mult); BigInteger bi = bd.toBigInteger(); StringBuilder str = new

DECIMAL length for microtime(true)?

限于喜欢 提交于 2019-12-04 18:49:41
问题 I want to store PHP's microtime as my timestamp in MySQL. I've been told it's best to store it in DECIMAL , but I can't find an ideal size. Does anyone know what the maximum size microtime(true) returns, so I can put that as my data type length? Should I choose variable DECIMAL length? 回答1: tl;dr. Use microtime(false) and store the results in a MySQL bigint as millionths of seconds. Otherwise you have to learn all about floating point arithmetic, which is a big fat hairball. The PHP microtime

How do I calculate percentages with decimals in SQL?

旧街凉风 提交于 2019-12-04 18:05:47
问题 How can i convert this to a decimal in SQL? Below is the equation and i get the answer as 78 (integer) but the real answer is 78.6 (with a decimal) so i need to show this as otherwise the report won't tally up to 100% (100 * [TotalVisit1]/[TotalVisits]) AS Visit1percent 回答1: convert(decimal(5,2),(100 * convert(float,[TotalVisit1])/convert(float,[TotalVisits]))) AS Visit1percent Ugly, but it works. 回答2: Try This: (100.0 * [TotalVisit1]/[TotalVisits]) AS Visit1percent 回答3: At least in MySQL (if

How do I format my percent variable to 2 decimal places?

亡梦爱人 提交于 2019-12-04 18:00:21
问题 This program is basically working with text files, reading the data & performing functions: while(s.hasNext()){ name= s.next(); mark= s.nextDouble(); double percent= (mark / tm )*100 ; System.out.println("Student Name : " +name ); System.out.println("Percentage In Exam: " +percent+"%"); System.out.println(" "); } I would like to format the percent value to 2 decimal places but since it's inside a while loop I cannot use the printf. 回答1: Elliot's answer is of course correct, but for

chop unused decimals with javascript

眉间皱痕 提交于 2019-12-04 17:22:10
问题 I've got a currency input and need to return only significant digits. The input always has two decimal places, so: 4.00 -> 4 4.10 -> 4.1 4.01 -> 4.01 Here's how I'm currently doing it: // chop off unnecessary decimals if (val.charAt(val.length-1) == '0') { // xx.00 val = val.substr(0, val.length-1); } if (val.charAt(val.length-1) == '0') { // xx.0 val = val.substr(0, val.length-1); } if (val.charAt(val.length-1) == '.') { // xx. val = val.substr(0, val.length-1); } which works, and has a

Validating decimal in C# for storage in SQL Server

ⅰ亾dé卋堺 提交于 2019-12-04 17:07:23
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 aside I have heard that SQL Server stores decimal in a completely different way to the CLR, is this true

convert Decimal array to Double array

Deadly 提交于 2019-12-04 16:56:14
问题 What's an efficient and hopefully elegant incantation to convert decimal[] to double[] ? I'm working with some fairly large arrays. 回答1: double[] doubleArray = Array.ConvertAll(decimalArray, x => (double)x); 回答2: You also can use and extension classes similar to this one public static class ArrayExtension { public static double[] ToDouble(this float[] arr) => Array.ConvertAll(arr, x => (double)x); } Then: double[] doubleArr = decimalArr.ToDouble(); 来源: https://stackoverflow.com/questions

Rails: Cannot add :precision or :scale options with change_column in a migration?

柔情痞子 提交于 2019-12-04 16:56:10
问题 This seems to have been asked before: rails decimal precision and scale But when running a change_column migration for :precision or :scale they don't actually affect the schema or database, but db:migrate runs without errors. My migration file looks like this: class ChangePrecisionAndScaleOfPaybackPeriodInTags < ActiveRecord::Migration def self.up change_column :tags, :payback_period, :decimal, { :scale => 3, :precision => 10 } end def self.down change_column :tags, :payback_period, :decimal

Mathematical explanation why Decimal's conversion to Double is broken and Decimal.GetHashCode separates equal instances

為{幸葍}努か 提交于 2019-12-04 16:33:10
问题 I am not sure if this non-standard way of stating a Stack Overflow question is good or bad, but here goes: What is the best (mathematical or otherwise technical) explanation why the code: static void Main() { decimal[] arr = { 42m, 42.0m, 42.00m, 42.000m, 42.0000m, 42.00000m, 42.000000m, 42.0000000m, 42.00000000m, 42.000000000m, 42.0000000000m, 42.00000000000m, 42.000000000000m, 42.0000000000000m, 42.00000000000000m, 42.000000000000000m, 42.0000000000000000m, 42.00000000000000000m, 42