-3

I am making a Windows Phone 8.1 App and I am trying to make a money counting function. I want to know if the variable has 1 decimal or 2 or more.

double 1decimal = 0.2;
double 2decimalsOrMore = 0.33333;

I need some kind of "if-statement" that checks if the variable has 1 or more decimals. If it has 1 decimal I need it to have 2 decimals by adding "0" to the double

if ("code here){
    // has 1 decimal
    string StringnewDecimalValue = oldDecimalValue + "0";
    double newDecimalValue = double.parse(newDecimalValue);
}
else{
    // has 2 decimals or more
    double newDecimalValue = Math.Round(oldDecimalValue , 2);
}

Basically, if the endresult (money) has 1 decimal (something like $1.2) it needs to be $1.20

Cheesebaron
  • 21,926
  • 13
  • 60
  • 110
Kevin
  • 44
  • 1
  • 4
  • ?? what why marked as a duplicate? – Kevin Nov 22 '14 at 23:06
  • 9
    use `decimal` instead of `double` when you are dealing with money. – L.B Nov 22 '14 at 23:09
  • 1
    http://stackoverflow.com/questions/890100/how-do-i-format-a-double-to-currency-rounded-to-the-nearst-dollar – Rufus L Nov 22 '14 at 23:13
  • 1
    What do you want `0.33333` to be displayed as? – Enigmativity Nov 22 '14 at 23:29
  • 2
    Also, I upvoted the comment about using `decimal` rather than `double`, but I think it deserves a second comment. Don't use `double` to represent currency amounts. Always use `decimal` instead. – Enigmativity Nov 22 '14 at 23:30
  • 3
    There are built-in format strings for the numeric types. If you want to show currency to 2 digits, all you need to do is decimalValue.ToString("c2"). – dbugger Nov 22 '14 at 23:40
  • I agree with the above use decimal rather than double when dealing with money and learn how to format as string - http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx – Shaun Wilde Nov 22 '14 at 23:55
  • BTW, your assumption that a number that doesn't enter a "has 1 decimal" if block must have two decimals or more is incorrect: it could be a whole number, in which appending `"0"` to the string representation of that number changes `"1"` to `"10"` instead of the intended `"1.00"`, and in which case rounding that two decimals and converting that to a string leaves it as `"1"`. You'd need a third branch if you continue attempting to do it manually. –  Nov 23 '14 at 00:03

2 Answers2

0

looks like a formatting problem.. so why not simply use string.format?

1 line of code: (based on this post Using String Format to show decimal upto 2 places or simple integer)

 var fixed = string.Format("{0:0.00}", oldDecimalValue);

and please make sure that your variables names don;y start with numeric character. instead of using:

 double 1decimal = 0.2;
 double 2decimalsOrMore = 0.33333;

please use:

 double decimal1 = 0.2;
 double decimalsOrMore2 = 0.33333;
Community
  • 1
  • 1
ymz
  • 4,053
  • 1
  • 14
  • 31
0

I'm confused by what you mean by "has two decimals"; if you are using actual double or decimal data types, the trailing zeros are meaningless. In order words:

decimal d1 = decimal.Parse("1.2");
decimal d2 = decimal.Parse("1.2000000");

produces the same value in both cases. (You should be using decimal if you're dealing with monetary values, as they can precisely represent base-10 numbers in a way that double cannot.)

However, if all you're concerned about is that printed string representation has two decimals, you don't need to do anything special; just use the appropriate formatting string:

var s = d1.ToString("c2");

will always print the training 0 if needed.

Michael Edenfield
  • 27,188
  • 4
  • 77
  • 114