String.Format

  • Thread starter Thread starter Jaroslav Jakes
  • Start date Start date
J

Jaroslav Jakes

Hi,

crazy, can't find a simple way.

Have a table with columns: ElementValue, ElementFormat (both string-format)

Would like to format ElementValue according to ElementFormat


e.g.
ElementValue: 0000545
ElementFormat: #####.##

Result: 5.45

Do you have an idea?

Thanks and regards

Jari
 
Hi Jari,

The problem is you don't have a decimal separator in your number, and to
my knowledge no formatting methods available will insert one based on a
format string.

You will have to create your own conversion code.
You don't say in what ways the elementvalue or elementformat may change,
but this is a general code that will transform the value to 5.45 based on
the format.

//format is elementformat, value is elementvalue
string result = "";
for(int i = 0, j = 0; i < format.Length; i++)
{
if(format == '#')
{
result += value[j];
j++;
}
else
result += format;
}
result = result.TrimStart('0');

If you need it to be a number you can just parse the result instead of
using TrimStart.
 
Hi Morten,

thanks for your feedback. As it seems, there is really no way without an
"format-engin".

Regards - Jari
 
Jari,

Don't be silly:

if your format does not have a decimal, but you know the precision, just
insert the decimal:

int myInt = 54332;
float myFloat = myInt/100.0;
Console.Writeline("{0}",myFloat);

This used to be really common in Cobol days, when a byte of storage was
really expensive.

--- Nick

Jaroslav Jakes said:
Hi Morten,

thanks for your feedback. As it seems, there is really no way without an
"format-engin".

Regards - Jari


Morten Wennevik said:
Hi Jari,

The problem is you don't have a decimal separator in your number, and to
my knowledge no formatting methods available will insert one based on a
format string.

You will have to create your own conversion code.
You don't say in what ways the elementvalue or elementformat may change,
but this is a general code that will transform the value to 5.45 based on
the format.

//format is elementformat, value is elementvalue
string result = "";
for(int i = 0, j = 0; i < format.Length; i++)
{
if(format == '#')
{
result += value[j];
j++;
}
else
result += format;
}
result = result.TrimStart('0');

If you need it to be a number you can just parse the result instead of
using TrimStart.
 
Hi Nick,

at runtime, I just have informatin about value and format. So value's
datatype is 'unknown'...

I was too lazy to store information about datatype, because I thought, that
formating would be the easiest part of all.

Thank's - Jari


Nick Malik said:
Jari,

Don't be silly:

if your format does not have a decimal, but you know the precision, just
insert the decimal:

int myInt = 54332;
float myFloat = myInt/100.0;
Console.Writeline("{0}",myFloat);

This used to be really common in Cobol days, when a byte of storage was
really expensive.

--- Nick

Jaroslav Jakes said:
Hi Morten,

thanks for your feedback. As it seems, there is really no way without an
"format-engin".

Regards - Jari


Morten Wennevik said:
Hi Jari,

The problem is you don't have a decimal separator in your number, and to
my knowledge no formatting methods available will insert one based on a
format string.

You will have to create your own conversion code.
You don't say in what ways the elementvalue or elementformat may change,
but this is a general code that will transform the value to 5.45 based on
the format.

//format is elementformat, value is elementvalue
string result = "";
for(int i = 0, j = 0; i < format.Length; i++)
{
if(format == '#')
{
result += value[j];
j++;
}
else
result += format;
}
result = result.TrimStart('0');

If you need it to be a number you can just parse the result instead of
using TrimStart.

 
Back
Top