-2

I have the amount

  var Amount = 23454;

And I need to Format it like this: 234.54

I searched for similar problems and tried this:

  String.Format("{0:0.00}", Amount / 100) // 234.00
  String.Format("{0:0.##}", Amount / 100) // 234
  (Amount / 100).ToString(CultureInfo.CreateSpecificCulture("en-GB")) // 234

but it removes decimals

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
onlyo
  • 53
  • 5
  • 3
    `Amount` is `int` so you're doing integer division. Until you can see that intuitively, you should avoid using `var`. It's only worth the convenience if it doesn't hide what you need to see. – madreflection Oct 19 '20 at 15:38
  • try `var Amount = 234.54m;` The m suffix denotes a decimal. – Ben Oct 19 '20 at 15:43
  • `string.Format("{0:0.00}", Amount / 100m) // 234.54` <= one of the operands has to be a floating-point type, otherwise you're doing integer division. The easiest way to to this in your example is to either define `Amount` as a `float`, `double` or `decimal`, or put a `f`, `d`, or `m` after the `100` to indicate that *that* number is a floating-point type – Rufus L Oct 19 '20 at 15:51

1 Answers1

1

This should do the trick:

String.Format("{0:0.##}", ((Decimal)Amount) / 100)

As mentioned before, you are working with an integer and not a decimal. You have to cast it before doing your logic

Kai
  • 732
  • 1
  • 7
  • 23