decimal

Arbitrary-Precision Decimals in C# [duplicate]

旧时模样 提交于 2019-12-17 04:08:08
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Big integers in C# C# unlimited significant decimal digits (arbitrary precision) without java I read the question at Arbitrary precision decimals in C#? but I don't have the J# library. I need a library for arbitrary precision decimals with C#. 回答1: Big Decimal: Install the J# runtime (it's free): http://www.microsoft.com/downloads/en/details.aspx?familyid=f72c74b3-ed0e-4af8-ae63-2f0e42501be1&displaylang=en Big

Write a number with two decimal places SQL server

梦想的初衷 提交于 2019-12-17 03:57:50
问题 How do you write a number with two decimal places for sql server? 回答1: try this SELECT CONVERT(DECIMAL(10,2),YOURCOLUMN) 回答2: Use Str() Function. It takes three arguments(the number, the number total characters to display, and the number of decimal places to display Select Str(12345.6789, 12, 3) displays: ' 12345.679' ( 3 spaces, 5 digits 12345, a decimal point, and three decimal digits (679). - it rounds if it has to truncate, (unless the integer part is too large for the total size, in

Regular expression for decimal number

天涯浪子 提交于 2019-12-17 03:40:09
问题 I need to validate a textbox input and can only allow decimal inputs like: X,XXX (only one digit before decimal sign and a precision of 3). I'm using C# and try this ^[0-9]+(\.[0-9]{1,2})?$ ? 回答1: ^[0-9]([.,][0-9]{1,3})?$ It allows: 0 1 1.2 1.02 1.003 1.030 1,2 1,23 1,234 BUT NOT: .1 ,1 12.1 12,1 1. 1, 1.2345 1,2345 回答2: There is an alternative approach, which does not have I18n problems (allowing ',' or '.' but not both): Decimal.TryParse. Just try converting, ignoring the value. bool

What does the M stand for in C# Decimal literal notation?

為{幸葍}努か 提交于 2019-12-17 03:34:15
问题 In order to work with decimal data types, I have to do this with variable initialization: decimal aValue = 50.0M; What does the M part stand for? 回答1: It means it's a decimal literal, as others have said. However, the origins are probably not those suggested elsewhere in this answer. From the C# Annotated Standard (the ECMA version, not the MS version): The decimal suffix is M/m since D/d was already taken by double . Although it has been suggested that M stands for money, Peter Golde recalls

range() for floats

て烟熏妆下的殇ゞ 提交于 2019-12-17 02:21:16
问题 Is there a range() equivalent for floats in Python? >>> range(0.5,5,1.5) [0, 1, 2, 3, 4] >>> range(0.5,5,0.5) Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> range(0.5,5,0.5) ValueError: range() step argument must not be zero 回答1: I don't know a built-in function, but writing one like this shouldn't be too complicated. def frange(x, y, jump): while x < y: yield x x += jump As the comments mention, this could produce unpredictable results like: >>> list(frange(0,

Is a double really unsuitable for money?

大憨熊 提交于 2019-12-17 02:10:36
问题 I always tell in c# a variable of type double is not suitable for money. All weird things could happen. But I can't seem to create an example to demonstrate some of these issues. Can anyone provide such an example? (edit; this post was originally tagged C#; some replies refer to specific details of decimal , which therefore means System.Decimal). (edit 2: I was specific asking for some c# code, so I don't think this is language agnostic only) 回答1: Very, very unsuitable. Use decimal. double x

Find number of decimal places in decimal value regardless of culture

。_饼干妹妹 提交于 2019-12-17 02:10:14
问题 I'm wondering if there is a concise and accurate way to pull out the number of decimal places in a decimal value (as an int) that will be safe to use across different culture info? For example: 19.0 should return 1, 27.5999 should return 4, 19.12 should return 2, etc. I wrote a query that did a string split on a period to find decimal places: int priceDecimalPlaces = price.ToString().Split('.').Count() > 1 ? price.ToString().Split('.').ToList().ElementAt(1).Length : 0; But it occurs to me

Why can't I unbox an int as a decimal?

最后都变了- 提交于 2019-12-17 01:01:46
问题 I have an IDataRecord reader that I'm retrieving a decimal from as follows: decimal d = (decimal)reader[0]; For some reason this throws an invalid cast exception saying that the "Specified cast is not valid." When I do reader[0].GetType() it tells me that it is an Int32. As far as I know, this shouldn't be a problem.... I've tested this out by this snippet which works just fine. int i = 3750; decimal d = (decimal)i; This has left me scratching my head wondering why it is failing to unbox the

How To Represent 0.1 In Floating Point Arithmetic And Decimal

旧时模样 提交于 2019-12-16 20:59:41
问题 I am trying to understand floating point arithmetic better and have seen a few links to 'What Every Computer Scientist Should Know About Floating Point Arithmetic'. I still don't understand how a number like 0.1 or 0.5 is stored in floats and as decimals. Can someone please explain how it is laid out is memory? I know about the float being two parts (i.e., a number to the power of something). 回答1: I've always pointed people towards Harald Schmidt's online converter, along with the Wikipedia

Getting the Nth Decimal of a Float

◇◆丶佛笑我妖孽 提交于 2019-12-14 04:17:17
问题 I was trying to compare between decimals in floats using if statements, but I don't know how to do it. I searched a lot but didn't get the answer. For example: If I have a float like this 5.26, how can I say if the 2nd decimal (which is 6) is bigger/smaller than (a certain number) or not?! I hope that it's clear to understand. RE-FORMING THE Qs: If I have a float that have 2 numbers after the decimal point, I want to check if the second number is bigger than "5" -for example- or not? 回答1: