L Luigi Z Nov 28, 2008 #1 Hi all, how can I get the absolute value of a decimal nullable value? (C# 2.0). Thanks a lot.
A Alberto Poblacion Nov 28, 2008 #2 Luigi Z said: how can I get the absolute value of a decimal nullable value? (C# 2.0). Click to expand... You can add to your code a function for this purpose: public decimal? Abs(decimal? d) { if (d.HasValue && d.Value<0) return -d.Value; return d; }
Luigi Z said: how can I get the absolute value of a decimal nullable value? (C# 2.0). Click to expand... You can add to your code a function for this purpose: public decimal? Abs(decimal? d) { if (d.HasValue && d.Value<0) return -d.Value; return d; }
L Luigi Z Nov 28, 2008 #3 Alberto Poblacion said: You can add to your code a function for this purpose: public decimal? Abs(decimal? d) { if (d.HasValue && d.Value<0) return -d.Value; return d; } Click to expand... Good solution, thank you Alberto. Luigi
Alberto Poblacion said: You can add to your code a function for this purpose: public decimal? Abs(decimal? d) { if (d.HasValue && d.Value<0) return -d.Value; return d; } Click to expand... Good solution, thank you Alberto. Luigi
L Luigi Z Jan 15, 2009 #4 Hi Alberto, a little similar question. How can I round a decimal nullable, like in this snippet: decimal? totalValue = null; foreach (decimal? value in valuesList) { if (!value.HasValue) continue; totalValue = totalValue ?? 0; totalValue += value; } return totalValue; The problem is to round "value" in totalValue += value; Have you idea? Thanks a lot Luigi
Hi Alberto, a little similar question. How can I round a decimal nullable, like in this snippet: decimal? totalValue = null; foreach (decimal? value in valuesList) { if (!value.HasValue) continue; totalValue = totalValue ?? 0; totalValue += value; } return totalValue; The problem is to round "value" in totalValue += value; Have you idea? Thanks a lot Luigi
A Alberto Poblacion Jan 15, 2009 #5 Luigi Z said: [...] foreach (decimal? value in valuesList) [...] totalValue += value; [...] The problem is to round "value" in totalValue += value; Have you idea? Click to expand... Simply get the Value of value (if it HasValue) and apply whatever rounding formula you desire to this (non-nullable) decimal. For instance: private decimal MyRound(decimal? v) { if (v.HasValue) { decimal d = v.Value; return decimal.Round(d,0); } return 0; } Now do: totalValue += MyRound(value);
Luigi Z said: [...] foreach (decimal? value in valuesList) [...] totalValue += value; [...] The problem is to round "value" in totalValue += value; Have you idea? Click to expand... Simply get the Value of value (if it HasValue) and apply whatever rounding formula you desire to this (non-nullable) decimal. For instance: private decimal MyRound(decimal? v) { if (v.HasValue) { decimal d = v.Value; return decimal.Round(d,0); } return 0; } Now do: totalValue += MyRound(value);