0

I've a predefined string format. For instance '>>>,>>>,>>9.99' this means that the system should display string in this '500,000,000.10'. The format can change based on the users using it. How can I write a common function to display stings on the given format passing the input value and the format as the parameter using C#

8 Answers 8

1

You can use the ToString method with a standard or custom format string

Sign up to request clarification or add additional context in comments.

Comments

1

For example:

string format = "{0:000,000,000.00}";
string val = 12.3456;
Console.WriteLine(string.Format(format, value)); // it prints "000,000,123.23"

You can read more about formating values here http://www.csharp-examples.net/string-format-double/

Comments

0
decimal value = 1.2345; 
string rounded = value.ToString("d2");

Comments

0
private string sDecimalFormat = "0.00";

decimal d = 120M;

txtText.Text = d.ToString(sDecimalFormat);

You could then have a setting for decimal format eg:

txtText.Text = d.ToString(Settings.DecimalFormat);

Comments

0

String.formate can be used for formating.

Go there if you want examples http://www.csharp-examples.net/string-format-double/

Comments

0

I think the following might work:

String result = String.Format(fmt.Replace('>', '#').Replace('9', '0'), inpString);

fmt being the format you want to use and inpString being the string entered by the user.

Just replace the > with # and the 9 with 0 and it'll be a valid .Net formatstring.

Comments

0

There is a Format method on String.

String.Format("{0:X}", 10); // prints A (hex 10)

There are several methods to format numbers, date...

Comments

0

I dont seem to understand how you can make 500,000,000.10 from >>>,>>>,>>9.99' but I believe the answer would be

But I assume something you are looking for is: string.Format("500,000,00{0:0.##}", 9.9915)

You can then make a method like

Public string GetString(string Format, object value)
{
     return string.Format(Format, value);
}

Something like this?

2 Comments

why would you need a "GetString" method to call string.format with the exact same parameters?
because the TS asked how to write a common function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.