What is the purpose of Decimal.One, Decimal.Zero, Decimal.MinusOne in .Net

Small clarification. They are actually static readonly values and not constants. That has a distinct difference in .Net because constant values are inlined by the various compilers and hence it’s impossible to track their usage in a compiled assembly. Static readonly values however are not copied but instead referenced. This is advantageous to your question … Read more

Generating a Random Decimal in C#

EDIT: Removed old version This is similar to Daniel’s version, but will give the complete range. It also introduces a new extension method to get a random “any integer” value, which I think is handy. Note that the distribution of decimals here is not uniform. /// <summary> /// Returns an Int32 with a random value … Read more

Why does adding two decimals in Javascript produce a wrong result? [duplicate]

It’s not a JS problem but a more general computer one. Floating number can’t store properly all decimal numbers, because they store stuff in binary For example: 0.5 is store as b0.1 but 0.1 = 1/10 so it’s 1/16 + (1/10-1/16) = 1/16 + 0.0375 0.0375 = 1/32 + (0.0375-1/32) = 1/32 + 00625 … … Read more

Convert string to decimal, keeping fractions

Hmm… I can’t reproduce this: using System; class Test { static void Main() { decimal d = decimal.Parse(“1200.00”); Console.WriteLine(d); // Prints 1200.00 } } Are you sure it’s not some other part of your code normalizing the decimal value later? Just in case it’s cultural issues, try this version which shouldn’t depend on your locale … Read more

DOUBLE vs DECIMAL in MySQL

Actually it’s quite different. DOUBLE causes rounding issues. And if you do something like 0.1 + 0.2 it gives you something like 0.30000000000000004. I personally would not trust financial data that uses floating point math. The impact may be small, but who knows. I would rather have what I know is reliable data than data … Read more

How can I format decimal property to currency?

Properties can return anything they want to, but it’s going to need to return the correct type. private decimal _amount; public string FormattedAmount { get { return string.Format(“{0:C}”, _amount); } } Question was asked… what if it was a nullable decimal. private decimal? _amount; public string FormattedAmount { get { return _amount == null ? … Read more

Converting double to string with N decimals, dot as decimal separator, and no thousand separator

For a decimal, use the ToString method, and specify the Invariant culture to get a period as decimal separator: value.ToString(“0.00”, System.Globalization.CultureInfo.InvariantCulture) The long type is an integer, so there is no fraction part. You can just format it into a string and add some zeros afterwards: value.ToString() + “.00”

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)