473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

String.Format

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


Nov 16 '05 #1
4 3521
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[i] == '#')
{
result += value[j];
j++;
}
else
result += format[i];
}
result = result.TrimStart('0');

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

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Hi Morten,

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

Regards - Jari
"Morten Wennevik" <Mo************@hotmail.com> schrieb im Newsbeitrag
news:opsh95qmuuklbvpo@pbn_computer...
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[i] == '#')
{
result += value[j];
j++;
}
else
result += format[i];
}
result = result.TrimStart('0');

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

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3
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" <jj****@eh-services.ch> wrote in message
news:eC**************@TK2MSFTNGP11.phx.gbl...
Hi Morten,

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

Regards - Jari
"Morten Wennevik" <Mo************@hotmail.com> schrieb im Newsbeitrag
news:opsh95qmuuklbvpo@pbn_computer...
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[i] == '#')
{
result += value[j];
j++;
}
else
result += format[i];
}
result = result.TrimStart('0');

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

--
Happy Coding!
Morten Wennevik [C# MVP]


Nov 16 '05 #4
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" <ni*******@hotmail.nospam.com> schrieb im Newsbeitrag
news:yu0rd.170718$HA.160394@attbi_s01...
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" <jj****@eh-services.ch> wrote in message
news:eC**************@TK2MSFTNGP11.phx.gbl...
Hi Morten,

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

Regards - Jari
"Morten Wennevik" <Mo************@hotmail.com> schrieb im Newsbeitrag
news:opsh95qmuuklbvpo@pbn_computer...
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[i] == '#')
{
result += value[j];
j++;
}
else
result += format[i];
}
result = result.TrimStart('0');

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

--
Happy Coding!
Morten Wennevik [C# MVP]



Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
3
by: Dominique Vandensteen | last post by:
after the very small & vs string.format discussion I did some speed tests... loop of 1.000.000 concatenations of 5 public string variables in a class gave following results: result = a & b...
4
by: David Morris | last post by:
Hi Could somebody please explain what the following line of code means String.Format("{0}\{1}.{2:00}", C:\, myfile.txt, 1 It's actually the first argument that I don't understand. What is...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
5
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
3
by: shawrie | last post by:
Hello everyone can anyone please help me? I basically want to set the length of a string variable to help spacing my simple report out. i tried dim test as string(14) but it didnt like that
5
by: jpenguin | last post by:
I'm doing a simple program to learn java. I have the input and math parts right. It just isn't displaying right. It outputs a list of about 11 things-- with the format "string $ xx.xx" In each...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.