user2125060
user2125060

Reputation: 45

Decimal formatting

I have a decimal = 123456 and an integer = 5 I want to insert "." at the fifth position of my decimal from the right and get 1.23456 How can I do this with standard formatting functions (i. e. without dividing by power of 10 and only then formatting to add missing zeros)? Thanks.

Upvotes: 1

Views: 227

Answers (4)

kashif
kashif

Reputation: 3834

You can do this by String.Insert

decimal d = 100000000000;
string str = d.ToString();
int i = 5;
string str2 = str.Insert(str.Length - i, ".");
Console.WriteLine(str2);
Console.Read();

Upvotes: 0

Meirion Hughes
Meirion Hughes

Reputation: 26398

This is very ugly; What is the real value? 12345 or 1.2345? Why are you storing 12345 and then trying to represent it as a different number? Going off what you are trying to convey what you actually have is an fixed-point (encoded) value and you need to decode it first. i.e.

decimal fixedPoint = 12345
decimaldecoded = fixedPoint / (decimal)10000
decoded.ToString();

So in your code you should define that you have a

var fixedPoint = new FixedPointValue(12345, 5);
var realValue = fixedPoint.Decode();

If any other programmer looks at this, it is plainly easy why you have to format it in such a way.

Upvotes: 0

Sconibulus
Sconibulus

Reputation: 732

This was actually pretty interesting, at least, I think it was. I hope I didn't go stupidly overboard by throwing in negative numbers, or accounting for possible decimal input...

            decimal input;
            int offset;
            string working = input.ToString();
            int decIndex = working.IndexOf('.');
            if (offset > 0)
            {
                if (decIndex == -1)
                {
                    working.PadLeft(offset, '0');
                    working.Insert(working.Length - offset, ".");
                }
                else
                {
                    working.Remove(decIndex, 1);
                    decIndex -= offset;
                    while (decIndex < 0)
                    {
                        working.Insert(0, "0");
                        decIndex++;
                    }
                    working.Insert(decIndex, ".");
                }
            }
            else if (offset < 0)
            {
                if (decIndex == -1)
                {
                    decIndex = working.Length();
                }
                if (decIndex + offset > working.Length)
                {
                    working.PadRight(working.Length - offset, '0');
                }
                else
                {
                    working.Remove(decIndex, 0);
                    working.Insert(decIndex + offset, ".");
                }

            }

Upvotes: 1

I4V
I4V

Reputation: 35353

Do you want something like this?

decimal d = 10000000;
int n=4;

string s = d.ToString();
var result = s.Substring(0, s.Length - n) + "." + s.Substring(s.Length - n);

Upvotes: 1

Related Questions