1
string input = Console.ReadLine();
decimal sum = Convert.ToDecimal(input);
if (sum >= (decimal)500.01)
{
    //40% and 8 dollars off shipping costs are taken off total amount
    decimal totalprice;
    totalprice = (sum - 8) * .60m;
    Math.Truncate(totalprice);
    Console.WriteLine("Your final cost is:${0:0.00}", totalprice);
    Console.Read();

The problem is, when I enter the price 598.88 dollars into my program, I should get 354.52.

The Math:

598.88 - 8 = 590.88. 590.88 * 60% = 354.528

I actually get 354.53 because C# rounds up instead of down. For example,

If I get an answer like 519.998, I want it to STAY at 519.99. Another example, if I get an answer like 930.755 I want it to stay at 930.75.

I looked into some answers, but Math.Truncate obviously doesn't work for me and using the *100 / 100 trick didn't work either. Keep in mind I'm a new student, So, if an answer could be noob-safe, that would be nice. Thanks.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
Sarah
  • 183
  • 1
  • 1
  • 10
  • 1
    Like zerkms said use Math.Floor(), also use Decimal.TryParse to make sure the input is fine. – user1534664 Nov 02 '12 at 00:45
  • `Math.Truncate()` obviously can't work for you, because it doesn't do anything to its argument, it returns the result. – svick Nov 02 '12 at 00:56

3 Answers3

2

The * 100 / 100 works fine, you might have been using it wrong. Try this below:

decimal totalprice = TruncateToTwoDigits((sum - 8) * .60m);
Console.WriteLine("Your final cost is:${0:0.00}", totalprice);

... 

private static decimal TruncateToTwoDigits(decimal Value)
{
    int adjusted = (int)Math.Truncate(Value * 100m);
    return adjusted / 100m;
}

As a side note, Math.Truncate returns the truncated value, it doesn't change the input parameter as your code would imply.

Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
  • Wow, that worked, and not too hard to understand but at a good level for me . Thanks for helping. – Sarah Nov 02 '12 at 01:14
2

Math.Truncate like all the other Math function returns the value after the function is called. The function doesn't alter your variable. Actually this was not possible with doubles (please see ref parameters). So you need to do:

totalprice = Math.Truncate(totalprice);

please notice that totalprice will have just the integer part so if the value is 45.985 the result is 45 so you need to multiply by 100 and then divide. http://msdn.microsoft.com/en-us/library/7d101hyf.aspx

The rounding up that you get there is because console.Write calls String.Format that will do that. See http://www.csharp-examples.net/string-format-double/ to get your write function call.

  • Wait, I thought code like "this.Close();" was called a method, which is a verb. Oh. And then you make a Method/Function out of the Main right? I got a lot to learn : – Sarah Nov 02 '12 at 19:03
0

Modulo works too. (It's hard to say which is better for safety, readability, and performance.)

Decimal totalprice = (sum - 8m) * 0.60m;  // Discount $8.00 and then 40%.
totalprice -= totalprice % 0.01;  // Truncate to two decimal places.

Similar question at Truncate Two decimal places without rounding

Community
  • 1
  • 1
A876
  • 471
  • 5
  • 8