'registering' format provider to format numbers as octal?

  • Thread starter Thread starter gjuro kladaric
  • Start date Start date
G

gjuro kladaric

hi there,

I am just learning about IFormattable, ICustomFormatter, IFormatProvider and
the like...

although being a bit tricky to understand, it becomes clear after some
clicking and readying...

what is not clear to me and constitutes my question is:

I can build 'format provider' and use it with .ToString() and .Format()
methods. However, Console.WriteLine() does not allow for any 'format
provider'. Can I somehow 'create/register/hack/build' a format provider into
the runtime system (for the lifetime of my application or forever), for (for
example) applicationwide or systemwide formatting numbers as octal, so I can
use transparently the following statement:

dim n as integer = 123
System.Console.WriteLine("{0:O}", n)

which would produce "173"

or

dim n as integer = 123
System.Console.WriteLine("{0:NumberToWordsFormat}", n)

which would produce "one hundred twenty three"
 
Hi,

Why not try this.

dim n as integer = 123
System.Console.WriteLine(String.Format("{0:O}", n))

Ken
 
hi, ken,

thanks for the answer

however, if I understand it well, your sample would not work, unless you
supply the format provider, like this:

dim n as integer = 123
System.Console.WriteLine(String.Format(format_provider, "{0:O}", n))

I am aware that I can do that, and also something like:

dim mc as myClass
mc.int_var = 123
System.Console.WriteLine(mc.int_var.ToStringOctal())

however, my question was if I can link my format provider (for octal
representation) into .NET runtime, so that my way of octal formatting of an
integer, for eaxmple, is available everywhere where traditional formatting
is available

do you know that answer?

thanks,

gjuro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top