-5

I am using a payment service that requires all it's charges be submitted as a whole number as such:

$205.01 submitted as 20501
$195.43 submitted as 19543
$42.06 submitted as 4206

I tried this first:

Convert.ToInt32(OrderTotal * 100);

But I found if OrderTotal = $120.01 then I ended up with 12000, with the hundreds place rounded. What I wanted to end up with is 12001. How do I perform this conversion without rounding?

webworm
  • 10,587
  • 33
  • 120
  • 217
  • Do you mean you ended up with 12000 (3 zeros) or 1200 (2 zeros)? – juharr Oct 07 '14 at 16:08
  • 2
    do `Convert.ToInt32(OrderTotal.ToString().Replace(".", ""))` :P – Habib Oct 07 '14 at 16:09
  • 1
    What is the type of your `OrderTotal` variable? – admdrew Oct 07 '14 at 16:10
  • 6
    If you started with 120.01 and multiplied by 100, and ended up with 1200 - then either your CPU is broken, or your code is simply wrong. I'm going to bet on the latter. – Marc Gravell Oct 07 '14 at 16:11
  • I just tried your code and when OrderTotal is `120.01` the resut is `12001`. – juharr Oct 07 '14 at 16:11
  • You may want to check out difference between casting and rounding http://www.bing.com/search?q=c%23+cast+int+vs+round which I believe what you actually talking about. – Alexei Levenkov Oct 07 '14 at 16:14
  • try to be sure that OrderTotal is correctly retrieved and has the right type – BRAHIM Kamel Oct 07 '14 at 16:21
  • Is `OrderTotal` _exactly_ `120.01` or could it be something like `120.005`? That figure would give `12000` using your code as it would be rounded using round to even. – petelids Oct 07 '14 at 16:48

4 Answers4

3
decimal firstDecimal = 120.01M;
double firstDouble = 120.01;
float firstFloat = 120.01F;

Console.WriteLine ((int)(firstDecimal * 100)); // 12001
Console.WriteLine ((int)(firstDouble * 100));  // 12001
Console.WriteLine ((int)(firstFloat * 100));   // 12001

Console.WriteLine (Convert.ToInt32(firstDecimal * 100)); // 12001
Console.WriteLine (Convert.ToInt32(firstDouble * 100));  // 12001
Console.WriteLine (Convert.ToInt32(firstFloat * 100));   // 12001

This means one thing.... you have something else going wrong with your code.

EDIT: Convert.ToInt32 produces the exact same result

Aydin
  • 15,016
  • 4
  • 32
  • 42
  • 1
    This is not a valid conclusion. Casting to int and using Convert.ToInt32() are not equivalent operations. The first truncates, the second rounds using banker's rounding. – Clever Neologism Oct 07 '14 at 16:35
  • One is casting and the other is converting, but in this case, it really doesn't make a difference. @CleverNeologism – Aydin Oct 07 '14 at 17:41
0

Your code is correct as per my view. If the OrderTotal is 120.01 then total = 120.01*100 = 12001.

double OrderTotal = 120.01;
int total = Convert.ToInt32(OrderTotal * 100);
Ripon
  • 141
  • 1
  • 8
0

You can multiply it by 100 then cast it as an it

(int)decaimalValue * 100
FJam
  • 736
  • 3
  • 13
  • 32
-1
    decimal firstDecimal = 120.01M;
    private int ConvertDecimalToInt(decimal value, short decimals)
    {
        if (decimals == 0) return (int)value;
        int valueInt = (int)(value * (decimal)Math.Pow(10, decimals));
        return valueInt;
    }
    Console.WriteLine(ConvertDecimalToInt(firstDecimal, 2)); // 12001