decimal

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

核能气质少年 提交于 2019-12-03 10:03:17
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 end end But my schema (and the data) remains as: t.decimal "payback_period" Anybody else have this

Interesting behaviour of type “decimal” in C#

我与影子孤独终老i 提交于 2019-12-03 09:50:30
If we declare padding as const decimal, the padding is not working. mymoney = 1.2 and your money = 1.20, how can this behavior be explained? class Program { static void Main(string[] args) { decimal balance = 1.2m; const decimal ConstPadding = 0.00m; decimal padding = 0.00m; decimal mymoney = decimal.Round(balance + ConstPadding, 2); decimal yourmoney = decimal.Round(balance + padding, 2); Console.WriteLine(mymoney); // 1.2 Console.WriteLine(yourmoney); //1.20 } } As an accompaniment to Jon's answer, below is the IL produced from your code. As he mentioned, mymoney was never added. .method

Truncate a floating point number without rounding up

℡╲_俬逩灬. 提交于 2019-12-03 09:43:26
I have a floating point number that I want to truncate to 3 places but I don't want to round up. For example, convert 1.0155555555555555 to 1.015 (not 1.016 ). How would I go about doing this in Ruby? Assuming you have a float , try this: (x * 1000).floor / 1000.0 Result: 1.015 See it working online: ideone You can also convert to a BigDecimal, and call truncate on it. 1.237.to_d.truncate(2).to_f # will return 1.23 Since ruby 2.4 Float#truncate method takes as an optional argument a number of decimal digits: 1.0155555555555555.truncate(3) # => 1.015 Multiply by a thousand, floor, divide by a

C# Type suffix for decimal

眉间皱痕 提交于 2019-12-03 09:26:16
I don't know what the correct wording is for what I am trying to achieve so it may already be posted online. Please be kind if it is. Ok so basically I have this method. public static T IsNull<T>(IDataReader dr, String name, T nullValue) { return Helpers.IsNull(dr, dr.GetOrdinal(name), nullValue); } public static T IsNull<T>(IDataReader dr, Int32 index, T nullValue) { if (dr.IsDBNull(index)) { return nullValue; } else { return (T)dr.GetValue(index); } } Being called as Helpers.IsNull(dr, "UnitWholeSale", 0d) and the error I am getting is "Cannot convert from double to decimal". Now I know I

How do I use decimal (float) in C++?

坚强是说给别人听的谎言 提交于 2019-12-03 08:53:41
According to IEEE 754-2008 there are There are three binary floating-point basic formats (which can be encoded using 32, 64 or 128 bits) and two decimal floating-point basic formats (which can be encoded using 64 or 128 bits). This chart is under it. In C++ I believe float and double are single and double precision ( binary32 and binary64 ). Name Common name Base Digits E min E max Digits E max binary32 Single precision 2 23+1 −126 +127 7.22 38.23 binary64 Double precision 2 52+1 −1022 +1023 15.95 307.95 binary128 Quadruple precision 2 112+1 -16382 +16383 34.02 4931.77 decimal32 10 7 −95 +96 7

Problems with Rounding Decimals (python)

孤人 提交于 2019-12-03 08:30:47
问题 In my program, decimal accuracy is very important. A lot of my calculations must be accurate to many decimal places (such as 50). Because I am using python, I have been using the decimal module (with context().prec = 99. ie; Set to have 99 decimal places of accuracy when instantiating a decimal object ) as pythonic floats don't allow anywhere near such accuracy. Since I wish for the User to specify the decimal places of accuracy of the calculations, I've had to implement several round()

Want to restrict the value of a MySQL field to specific range (Decimal values)

柔情痞子 提交于 2019-12-03 07:45:01
I want to restrict the value of a field in a row of a table to a specific range. Is it possible to restrict my relationship_level field to [0.00 to 1.00] ? At the moment I am using DECIMAL(2,2), it wouldn't allow DECIMAL(1,2) as M must be >= D. I assume a data type of DECIMAL(2,2) will actually allow values from 00.00 up to 99.99? CREATE TABLE relationships ( from_user_id MEDIUMINT UNSIGNED NOT NULL, to_user_id MEDIUMINT UNSIGNED NOT NULL, relationship_level DECIMAL(2,2) UNSIGNED NOT NULL, PRIMARY KEY (from_user_id, to_user_id), FOREIGN KEY (from_user_id) REFERENCES users (user_id) ON DELETE

What the best ways to use decimals and datetimes with protocol buffers?

自古美人都是妖i 提交于 2019-12-03 07:26:48
I would like to find out what is the optimum way of storing some common data type that were not included in the list supported by protocol buffers. datetime (seconds precision) datetime (milliseconds precision) decimals with fixed precision decimals with variable precision lots of bool values (if you have lots of them it looks like you'll have 1-2 bytes overhead for each of them due to their tags. Also the idea is to map them very easy to corresponding C++/Python/Java data types. The protobuf design rationale is most likely to keep data type support as "native" as possible, so that it's easy

pandas read_csv column dtype is set to decimal but converts to string

徘徊边缘 提交于 2019-12-03 07:08:21
I am using pandas (v0.18.1) to import the following data from a file called 'test.csv': a,b,c,d 1,1,1,1.0 I have set the dtype to 'decimal.Decimal' for columns 'c' and 'd' but instead they return as type 'str'. import pandas as pd import decimal as D df = pd.read_csv('test.csv', dtype={'a': int, 'b': float, 'c': D.Decimal, 'd': D.Decimal}) for i, v in df.iterrows(): print(type(v.a), type(v.b), type(v.c), type(v.d)) Results: `<class 'int'> <class 'float'> <class 'str'> <class 'str'>` I have also tried converting to decimal explicitly after import with no luck (converting to float works but not

Get Last 2 Decimal Places with No Rounding

狂风中的少年 提交于 2019-12-03 06:56:14
In C#, I'm trying to get the last two decimal places of a double with NO rounding. I've tried everything from Math.Floor to Math.Truncate and nothing is working. Samples of the results I'd like: 1,424.2488298 -> 1,424.24 53.5821 -> 53.58 10,209.2991 -> 10,209.29 Any ideas? Well, mathematically it's simple: var f = 1.1234; f = Math.Truncate(f * 100) / 100; // f == 1.12 Move the decimal two places to the right, cast to an int to truncate, shift it back to the left two places. There may be ways in the framework to do it too, but I can't look right now. You could generalize it: double Truncate