decimal

custom format for decimal c#

不打扰是莪最后的温柔 提交于 2019-12-10 18:05:33
问题 I'm trying to format a decimal value with decimals to a custom format without comas or points , Having checking http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx but can't find the format I need I need to convert a decimal value for example 3.1416 to 314 or even better 0000000314, any clue? 回答1: To scale by 100 and show up to 9 leading zeros use String.Format("{0:0000000000}", (value * 100)); 回答2: For just display String.Format("{0:##########}", (value * 100)) 回答3: Make a simple Method

Why does specifying a numeric value for a control's property in the designer result in new decimal(new int[] {… in the code?

[亡魂溺海] 提交于 2019-12-10 17:46:23
问题 In my C#.Net WinForm app in VS2005, I have a NumericUpDown control. In the designer, I set the Minimum property of the NumericUpDown to -65535. In the form's .designer.cs code, I expected to see this: this.myNumericUpDown.Minimum = -65535; but instead I get: this.myNumericUpDown.Minimum = new decimal(new int[] { 65535, 0, 0, -2147483648}); I know what this means from looking up the int32[] decimal constructor, but why does the designer use that form instead of putting the value I entered, or

C# Protobuf-net: Dictionary of decimals: Zeroes don't get roundtrip properly

*爱你&永不变心* 提交于 2019-12-10 17:38:29
问题 I've found a weird bug around serialization/deserialization of decimal zeroes in protobuf-net, wondering if anyone has found a good workaround for this, or if this is actually a feature. Given a dictionary like above, if i run in linqpad: void Main() { { Dictionary<string, decimal> dict = new Dictionary<string, decimal>(); dict.Add("one", 0.0000000m); DumpStreamed(dict); } { Dictionary<string, decimal> dict = new Dictionary<string, decimal>(); dict.Add("one", 0m); DumpStreamed(dict); } }

XmlSerializer to deserialize decimal with comma(,) decimal symbol

纵然是瞬间 提交于 2019-12-10 17:26:36
问题 I'd like to know if there is any simple way to deserialize decimal with comma decimal separator using XmlSerialier? I'am getting exported data in xml format from other software and all numbers(price, quantity, discount and many many more) in xml file has comma as decimal separator. Here is simplified example what I have tried to do: Lets say there is Product class with Name and Price: [Serializable] public class Product { public string Name { get; set; } public decimal Price { get; set; } }

MS SQL Float Decimal Comparison Problems

怎甘沉沦 提交于 2019-12-10 15:40:20
问题 I'm in the process of normalising a database, and part of this involves converting a column from one table from a FLOAT to a DECIMAL(28,18) . When I then try to join this converted column back to the source column, it returns no results in some cases. It seems to bee something to do with the ways its converted. For example, the FLOAT converted to a DECIMAL(28,18) produces: 51.051643260000006000 The original FLOAT is 51.05164326 I have tried various ways of modifying the FLOAT , and none of

Decimal places in SQL

安稳与你 提交于 2019-12-10 15:31:12
问题 I am calculating percentages. One example is coming down to 38589/38400 So the percentage is 100*(38589/38400) which equals something like 100.4921875, but the result shows up as 100. How can I get it to be displayed with x number of decimals? Similarly, will the same work if i'd like 2 to be displayed as 2.000000? Thanks! 回答1: You can cast it to a specific data type, which preserves the data type as well as rounding to a certain precision select cast(100*(38589/38400) as decimal(10,4)) FYI

How to calculate percentages of sum of sub-arrays so that their sum is strictly 100 using C# decimal

空扰寡人 提交于 2019-12-10 15:25:30
问题 Let's say I have an array of integers. Lets logically divide it into just two parts for simplification. Let's say total sum of all elements of array is 2860800 . Let's say sum of all elements of left half is 2834387 . Let's say sum of all elements of right half is 26413 . Now calculating left half's percentage by 100m * leftHalfSum / totalSum gives 99.07672678970917225950782998 . Similarly, right half's percentage by 100m * rightHalfSum / totalSum is 0.9232732102908277404921700224 . Now, if I

Python's behavior for rich comparison (Or, when Decimal('100.0') < .01)

淺唱寂寞╮ 提交于 2019-12-10 15:11:45
问题 So I have a one liner: import decimal; h = decimal.Decimal('100.0'); (h > .01, h < .01, h.__gt__(.01), h.__lt__(.01)) All it does is make a Decimal object holding 100.0, and compares it to .01 (the float) in various ways. My result is: >>> import decimal; h = decimal.Decimal('100.0'); (h > .01, h < .01, h.__gt__(.01), h.__lt__(.01)) (False, True, NotImplemented, NotImplemented) From the docs: "A rich comparison method may return the singleton NotImplemented if it does not implement the

Python Format Decimal with a minimum number of Decimal Places

孤者浪人 提交于 2019-12-10 15:01:33
问题 I have some Decimal instances in Python. I wish to format them such that Decimal('1') => '1.00' Decimal('12.0') => '12.00' Decimal('314.1') => '314.10' Decimal('314.151') => '314.151' hence ensuring that there are always at least two decimal places, possibly more. While there are no shortage of solutions for rounding to n decimal places I can find no neat ways of ensuring a lower bound on the number. My current solution is to compute: first = '{}'.format(d) second = '{:.2f}'.format(d) and

Mysql decimal: floor instead of round

痞子三分冷 提交于 2019-12-10 14:24:28
问题 In my MySQL database I have field DECIMAL(23,5), so 5 digits after decimal point. Now, when I will query like this: UPDATE my_table SET my_decimal_field = 123.123456789 WHERE id = 1 And then I will fetch that record: SELECT id, my_decimal_field FROM gijhars WHERE id = 1 I get this result: +------+------------------+ | id | my_decimal_field | +------+------------------+ | 5733 | 123.12346 | +------+------------------+ So, of course, MySQL is rounding these values, if they have more than 6