views:

292

answers:

7

Hi everyone,

I have to deal with a numeric value (coming from a SQL Server 2005 numeric column) which I need to convert to a string, with the caveat that the decimal point has to be removed. The data itself is accessible in C# via a DataTable/DataRow.

For example, if the SQL Server column reads 12345.678, the string should be "12345678" when all is said and done. Would it be possible to point me toward a good approach for this? Maybe there's a .Net conversion method that does this, I'm not sure.

Thanks!

+1  A: 

A straightforward method is to use

string result = original.Replace(".", "")

There might be a faster method, but if this isn't in a tight inner loop, Replace should work fine.

EDIT: Fixed code example, plus:

To address culture concerns, one might use NumberFormatInfo.NumberDecimalSeparator to determine the currently defined decimal separator.

Eric J.
ha! beated me by a second!
Francisco Noriega
Wow glad I had that extra cup of coffee this morning ;-)
Eric J.
Although there is no Replace that takes 3 params.
Francisco Noriega
Should be `original.Replace(".", "")`. ;)
David Brown
You haven't specified how the number is converted to a string in the first place. If the conversion uses a culture setting that uses something other than a period as decimal separator, this doesn't work.
Guffa
+3  A: 

what about something like

var numberWithPoint = dbGetNumber();

string numberWithOutPoint = numberWithPoint.ToString().Replace(".", string.Empty);

it's quick and dirty, but it get the job done fairly simply.

Francisco Noriega
...and it doesn't work if the current culture uses something other than period as decimal separator.
Guffa
The OP never specified the handling of multiple cultures as a requirement.
David Brown
@David: He asked for "a good approach", and I don't consider it a good approach if it breaks when the current culture changes...
Guffa
I was thinking of something along these lines....thanks for the tip. As for culture information, it _shouldn't_ be an issue, but it's something to keep in mind.Once again, StackOverflow shows itself to be a great resource. Thanks to everyone for your advice and comments!
larryq
Thanks @larryq, although I would actually change the "" for string.empty, that's what I usually use, but forgot in the rush :P
Francisco Noriega
+2  A: 

you can do it in c# like:

var myNum = 12345.678;
var myString = myNum.ToString().Replace(".","");

in sql server, you can do it like:

SELECT REPLACE( cast(myCol as varchar), '.', '') as FormattedNumber 
FROM ...
dan
The C# version doesn't work if the current culture uses something other than period as decimal separator.
Guffa
the decimal can always be replaced with `CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator` if culture is an issue, though it wasn't mentioned.
dan
+2  A: 

What about:

// using System.Globalization;
SqlCommand cm = new SqlCommand("SELECT 12345.678 as decimal");
// ...
SqlDataReader dr = cm.ExecuteReader();
if(dr.Read())
{
    string value = dr.GetValue(0).ToString()
        .Replace(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, "")
}
Rubens Farias
can I ask why downvote?
Rubens Farias
+1 - Why was this down voted? SqlDataReader isn't that scary....
Baddie
Just put `SqlDataReader` because OP said "numeric value coming from a SQL Server"; and this version deals with culture issues
Rubens Farias
+4  A: 

There are several possible approaches. You could convert it to a string using a specific culture so that you are sure that the period is used as decimal separator, then remove the period:

string s = num.ToString(CultureInfo.InvariantCulture).Replace(".", String.Empty);

You could use a more numeric approach to multiply the valye by 10 until there are no decimals:

while (num != Math.Floor(num)) num *= 10.0;
string s = ((int)num).ToString();
Guffa
+1 cleaver Math solution
Rubens Farias
Very clever. Thanks Guffa.
larryq
+1  A: 

but usually when it comes from sql server, you should not convert it first to a string than to integer / double but convert it directly from object in row["column1"] to needed value, this will save you troubles handling cultures and improve performance a bit

zebra
+1  A: 

string result = Regex.Replace(original, "[^0-9]", "");

BigSlick