decimal

Why does .NET decimal.ToString(string) round away from zero, apparently inconsistent with the language spec?

拜拜、爱过 提交于 2019-12-03 22:43:11
I see that, in C#, rounding a decimal , by default, uses MidpointRounding.ToEven . This is expected, and is what the C# spec dictates. However, given the following: A decimal dVal A format string sFmt that, when passed in to dVal.ToString(sFmt) , will result in a string containing a rounded version of dVal ...it is apparent that decimal.ToString(string) returns a value rounded using MidpointRounding.AwayFromZero . This would appear to be a direct contradiction of the C# spec. My question is this: is there a good reason this is the case? Or is this just an inconsistency in the language? Below,

validate decimal numbers

孤街醉人 提交于 2019-12-03 22:41:57
I want to validate that a number has certain parameters, for example I want to ensure that a number has 3 decimals is positive. I have searched in different places over the internet, although I could not find how to do it. I have made that text box to accept numbers only. I just need the rest of the features. Thanks, $("#formEntDetalle").validate({ rules: { tbCantidad: { required: true, number: true }, tbPrecioUnidad: { required: true, number: true }, } messages: { tbCantidad: { required: "Es Necesario Entrar una cantidad a la orden" }, tbPrecioUnidad: { required: "Es Necesario Entrar el valor

Decimal.TryParse doesn't parse my decimal value

巧了我就是萌 提交于 2019-12-03 22:31:43
When I tried to convert something like 0.1 (from user in textbox), My value b is always false. bool b = Decimal.TryParse("0.1", out value); How can it be here to work? Too late to the party, but I was going to suggest forcing the culuture to en-US but Invariant is a better sln decimal value; bool b = Decimal.TryParse("0.1", NumberStyles.Any, new CultureInfo("en-US"), out value); Specify the culture for the parsing. Your current culture uses some different number format, probably 0,1 . This will successfully parse the string: bool b = Decimal.TryParse("0.1", NumberStyles.Any, CultureInfo

Convert hex to decimal in R

半世苍凉 提交于 2019-12-03 22:14:55
I found out that there is function called .hex.to.dec in the fBasics package. When I do .hex.to.dec(a) , it works. I have a data frame with a column samp_column consisting of such values: a373, 115c6, a373, 115c6, 176b3 When I do .hex.to.dec(samp_column) , I get this error: "Error in nchar(b) : 'nchar()' requires a character vector" When I do .hex.to.dec(as.character(samp_column)) , I get this error: "Error in rep(base.out, 1 + ceiling(log(max(number), base = base.out))) : invalid 'times' argument" What would be the best way of doing this? Simon O'Hanlon Use base::strtoi to convert hexadecimal

What is a good mapping of .NET decimal to SQL Server decimal?

↘锁芯ラ 提交于 2019-12-03 22:04:24
I am having to interface some C# code with SQL Server and I want to have exactly as much precision when storing a value in the database as I do with my C# code. I use one of .NET's decimal type for a value. What datatype/precision would I use for this value in SQL Server? I am aware that the SQL Server decimal type is the type that most likely fits my needs for this. My question though is what scale and precision do I use so that it matches .NET's decimal type? You can use the SQL Server decimal type ( documentation here ). That allows you to specify a precision and scale for the numbers in

Rounding a variable to two decimal places C# [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-03 22:02:34
This question already has answers here : Closed 7 years ago . Possible Duplicate: How do you round a number to two decimal places in C#? I am interested in how to round variables to two decimal places. In the example below, the bonus is usually a number with four decimal places. Is there any way to ensure the pay variable is always rounded to two decimal places? pay = 200 + bonus; Console.WriteLine(pay); Use Math.Round and specify the number of decimal places. Math.Round(pay,2); Math.Round Method (Double, Int32) Rounds a double-precision floating-point value to a specified number of fractional

Using DEC2BIN() with large numbers

柔情痞子 提交于 2019-12-03 19:56:17
问题 I'm trying to convert 4503599627370495 into binary in Excel. DEC2BIN() returns #NUM! error because DEC2BIN cannot handle such a large number. Any thoughts on how I might be able to make it work? 回答1: Thanx JustinDavies - that was just what I needed, but it went into an endless loop if passed a -ve number. My modification: Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String DecToBin = "" DecimalIn = CDec(DecimalIn) If DecimalIn < 0 Then DecToBin = "Error -

How to multiply in Javascript? problems with decimals

旧城冷巷雨未停 提交于 2019-12-03 18:57:17
问题 i've the following code in Javascript: var m1 = 2232.00; var percent = (10/100); var total = percent*m1; alert(total); The problem is that the variable "total" gives me "223.20000000000002" and it should be "223.2", what should i do to get the correct value? 回答1: .toFixed() is best solution.It will keep only two digits after dot. Exp 1: var value = 3.666; value.toFixed(2); //Output : 3.67 Exp 2: var value = 3.0000; value.toFixed(2); //Output : 3.00 回答2: You can't get the exact value. This is

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

时光总嘲笑我的痴心妄想 提交于 2019-12-03 17:35:30
问题 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. 回答1: The

Why django uses a comma as decimal separator

五迷三道 提交于 2019-12-03 16:36:43
问题 I am using python 2.6 and django 1.27 my model class Plan(models.Model): price = models.DecimalField(max_digits=5, decimal_places=2,default=0) ..... in my template i have {{plan.price}} My problem is that on my local machine i get dot used as separator for example '2.54' While on my production machine i get '2,54' - comma is used as separator. I would like it to use dot everywhere. in the django docs https://docs.djangoproject.com/en/1.2/ref/settings/#decimal-separator it say there is the