0

Where my problem lies is if a user writes 200 then it makes a mistake.

But that error only because I have for example written 200.

I have tried different things that have led to it approving only the number 200 if it is.

My Models:

[Required(ErrorMessage = "Money")]
[RegularExpression(@"\d+(\.\d{1,2})?", ErrorMessage = "Error")]
[Range(0, 99999.99, ErrorMessage ="Error")]
[DataType(DataType.Text)]
[Display(Name = "Money")]
public string Pay { get; set; }

in my CompaniesController i get the value from "Pay"

decimal PayValue = model.Pay.ToString().PayValue();

And thus, in PayValue, I have done so that it divides the price in proportion to having to throw "," in between two numbers.

public static decimal PayValue(this string str)
    {
        string[] pay;
        pay = str.ToString().Split('.');
        return Convert.ToDecimal(pay[0] + "," + pay[1]);
    }

Here I would like it to run through and be successful.

  • Error here: 200
  • Error here: 20000

Thus, I would like if a customer writes the two numbers oven over then it must itself add ,00 only if nothing ,00 is add after the whole number.

Code fails if I only write 200, What I want it to be is that it must go through and even add ,00 after the number.

  • If I'm understanding correctly you are just wanting to show the amount formatted as currency? If so check this out https://stackoverflow.com/questions/4842332/currency-format-for-display – akaBase Jul 05 '19 at 19:12
  • Why convert to `decimal`? Why replace `'.'` with `','`? – Dialecticus Jul 05 '19 at 19:12
  • This is a case where I would recommend a unit test as the universal language. It's very, very hard to understand the problem you're trying to solve. But if you expressed it as a failing unit test (maybe a parameterized test) then there would be no barriers. – Scott Hannen Jul 05 '19 at 19:28
  • Thanks @akaBase It helped solve the problem for me. – J. Petersen Jul 05 '19 at 21:03

0 Answers0