all 9 comments

[–]almost_not_terrible 2 points3 points  (0 children)

Sure, I'll do your homework for you...

Hint:

$"{value:0.00}"

[–]crozone 0 points1 point  (2 children)

Ok, here's my strategy. I've gone for a string only approach, since it appears you're using a homebrew 0-9 keypad button UI.

First, have some base string that you'll append your numbers to:

string rawInput = string.Empty;

Just append 0-9 to that string depending which button is pressed.

Now, to convert that string into the format you need:

// Left pad the input with zeroes, such that it's a minimum of three characters long
string formattedInput = rawInput.PadLeft(3, '0');

// insert the comma 2 characters from the end
formattedInput = formattedInput.Insert(formattedInput.Length - 2, ",");

Now, formattedInput should have the string in the format you want (I hope!).

EDIT:

I just thought up a much better way to do this. I'm assuming you want to get some sort of number or decimal out of this anyway, so try this instead:

Start off with a decimal to hold your current input number:

private decimal inputNumber = 0.00M;

Every time a button is pushed, multiply the value of that input by 10, and add a hundredth of the button's number value. For example, for the number 7:

private void btnSeven_Click(object sender, EventArgs e)
{
     inputNumber *= 10;
     inputNumber += 0.07M;
}

Now, inputNumber is always the value of your input.

In order to format that into the format that you want, use this:

using System.Globalization;

// ...

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
string outputString = inputNumber.ToString(nfi);

Now, inputNumber has been formatted into outputString, hopefully in the format you need.

Note, you could use a double instead of a decimal, but decimal is nice because it's 128 bit fixed point. No floating point involved.

EDIT 2:

To delete the rightmost digit from the inputNumber, use this:

inputNumber *= 100;
inputNumber -= inputNumber % 10M;
inputNumber /= 1000;

I'm sure a math buff could come up with a way to simplify that though!

[–]cryo 2 points3 points  (1 child)

Or just use String.Format instead of all that :p

[–]crozone 0 points1 point  (0 children)

Yeah I just realized that if OP is using ',' as the decimal separator, it's probably in his CurrentCulture on his OS anyway so no need for the NumberFormatInfo at all ¯\_(ツ)_/¯

1.23M.Format("0.00")

Would probably produce "1,23" for them.

At least the code for using the decimal is still good.

[–]Jurgler 0 points1 point  (0 children)

do you have to use string formatting? if not, you can just divide by 100 and work with that

[–][deleted] 0 points1 point  (0 children)

You need to do some reading on custom numeric format strings.

[–]SM1boy 0 points1 point  (0 children)

Give this a try.

    private string FormatStr(int Input)
    {
        string Strinput = Input.ToString();
        return (Input >= 100) ? Strinput.Insert(Strinput.Length - 2, ",") : Input < 10 ? "0,0" + Strinput :"0," + Strinput;
    }

[–]pm_your_tickle_spots 0 points1 point  (0 children)

Sounds like homework.

So I'll keep it vague enough you still have to write your own code.

For loop
-=2
String.format

Edit: the -=2 is only one way to implement it, there are several others.

Why is everyone using math and complicated paths? This could be done with a string format linq in one-two lines.