473,385 Members | 2,015 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.