473,225 Members | 1,311 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,225 software developers and data experts.

C# - C sharp - String to decimal, decimal point disappears!

BA
Hello,

I have a string with a price in: "$14.95"

I need to get it into a decimal.

So I regex 'd the string and dumped the $. Debug mon shows a clean string
"14.95"

Then I do a System.Convert.ToDecimal on the clean string and the output
becomes: 1495

What gives!? I've tried a variety of workarounds but nothing works. How
can I format it for 2 decimal places or perform the convert in a way I am
not aware of?

Thanks alot!


Jul 21 '05 #1
7 2990
Show us the code. Copy it from your program. Might be a syntax issue.

Jul 21 '05 #2
BA

"Peter Mancini" <pe***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Show us the code. Copy it from your program. Might be a syntax issue.


Thanks for the response, here is the code (cutout try catch for ease of
reading, other funciton just returns the decimal:

>>>>>>>>>>>code>>>>>>>>>>>>>>>>
public decimal Lookup(string ISBN)
{
string CleanPrice;
decimal decCleanPrice;

System.Diagnostics.Trace.WriteLine("AmazonWebServi ceDLL:AmazonLookupAdapter
ENTER");

//Create Instance of Proxy Class
SService as1 = new SService ();

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchRequest(asin);

CleanPrice =
System.Text.RegularExpressions.Regex.Replace(pi.De tails[0].OurPrice,System.Text.RegularExpressions.Regex.Esc ape("$"),
"");
System.Diagnostics.Trace.WriteLine("price: " + CleanPrice);

decCleanPrice = ConvertStringDecimal(CleanPrice.ToString());

System.Diagnostics.Trace.WriteLine(System.Convert. ToString(decCleanPrice));
System.Diagnostics.Trace.WriteLine("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDecimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert.ToDecimal(stringVal);
System.Diagnostics.Trace.WriteLine("The string as a decimal is {0}.",
decimalVal.ToString());
return decimalVal;

}
catch (System.OverflowException)
{
System.Diagnostics.Trace.WriteLine("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatException)
{
System.Diagnostics.Trace.WriteLine("The string is not formatted as a
decimal.");
return 0;
}
catch (System.ArgumentNullException)
{
System.Diagnostics.Trace.WriteLine("The string is null.");
return 0;
}
}

}
}
>>>>>>>>code>>>>>>>>>>>>>>>>


Thanks again
Jul 21 '05 #3
SB
I don't see the problem. The output I get is 14.95.

-sb

// Method simplified for clarity
internal decimal ConvertStringToDecimal(string stringVal)
{
try
{
return System.Convert.ToDecimal(stringVal.TrimStart('$')) ; // remove
the "$", convert it to a decimal, then return it
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}
return 0;
}

"BA" <bi***************@gmail.com> wrote in message
news:1109726232.92275607742901ff95e2563b42b197c8@t eranews...

"Peter Mancini" <pe***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Show us the code. Copy it from your program. Might be a syntax issue.


Thanks for the response, here is the code (cutout try catch for ease of
reading, other funciton just returns the decimal:

>>>>>>>>>>>>code>>>>>>>>>>>>>>>>
public decimal Lookup(string ISBN)
{
string CleanPrice;
decimal decCleanPrice;
System.Diagnostics.Trace.WriteLine("AmazonWebServi ceDLL:AmazonLookupAdapter
ENTER");

//Create Instance of Proxy Class
SService as1 = new SService ();

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchRequest(asin);

CleanPrice =
System.Text.RegularExpressions.Regex.Replace(pi.De tails[0].OurPrice,System.Text.RegularExpressions.Regex.Esc ape("$"),
"");
System.Diagnostics.Trace.WriteLine("price: " + CleanPrice);

decCleanPrice = ConvertStringDecimal(CleanPrice.ToString());
System.Diagnostics.Trace.WriteLine(System.Convert. ToString(decCleanPrice));
System.Diagnostics.Trace.WriteLine("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDecimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert.ToDecimal(stringVal);
System.Diagnostics.Trace.WriteLine("The string as a decimal is {0}.",
decimalVal.ToString());
return decimalVal;

}
catch (System.OverflowException)
{
System.Diagnostics.Trace.WriteLine("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatException)
{
System.Diagnostics.Trace.WriteLine("The string is not formatted as a
decimal.");
return 0;
}
catch (System.ArgumentNullException)
{
System.Diagnostics.Trace.WriteLine("The string is null.");
return 0;
}
}

}
}
>>>>>>>>>>code>>>>>>>>>>>>>>>>


Thanks again

Jul 21 '05 #4
Does your system use the . (dot) symbol as the decimal symbol? Some european
country use the comma (,) as decimal symbol, and dot (.) as the digit
grouping symbol. If you're in the latter case, the conversion will ignore
the dot and treat the "14.95" as "1495". You can solve this by using
specific CultureInfo object to specify the locale that you want to use.

Hope it helps.

-- Ricky Lee
============================================
^o^ "When all doors are closed, God will open a Windows" ^o^
============================================

"BA" <bi***************@gmail.com> wrote in message
news:1109726232.92275607742901ff95e2563b42b197c8@t eranews...

"Peter Mancini" <pe***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Show us the code. Copy it from your program. Might be a syntax issue.

Thanks for the response, here is the code (cutout try catch for ease of
reading, other funciton just returns the decimal:

>>>>>>>>>>>>code>>>>>>>>>>>>>>>>
public decimal Lookup(string ISBN)
{
string CleanPrice;
decimal decCleanPrice;

System.Diagnostics.Trace.WriteLine("AmazonWebServi ceDLL:AmazonLookupAdapter ENTER");

//Create Instance of Proxy Class
SService as1 = new SService ();

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchRequest(asin);

CleanPrice =
System.Text.RegularExpressions.Regex.Replace(pi.De tails[0].OurPrice,System.T
ext.RegularExpressions.Regex.Escape("$"), "");
System.Diagnostics.Trace.WriteLine("price: " + CleanPrice);

decCleanPrice = ConvertStringDecimal(CleanPrice.ToString());

System.Diagnostics.Trace.WriteLine(System.Convert. ToString(decCleanPrice)); System.Diagnostics.Trace.WriteLine("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDecimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert.ToDecimal(stringVal);
System.Diagnostics.Trace.WriteLine("The string as a decimal is {0}.",
decimalVal.ToString());
return decimalVal;

}
catch (System.OverflowException)
{
System.Diagnostics.Trace.WriteLine("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatException)
{
System.Diagnostics.Trace.WriteLine("The string is not formatted as a
decimal.");
return 0;
}
catch (System.ArgumentNullException)
{
System.Diagnostics.Trace.WriteLine("The string is null.");
return 0;
}
}

}
}
>>>>>>>>>>code>>>>>>>>>>>>>>>>


Thanks again

Jul 21 '05 #5
Try
System.Globalization.NumberFormatInfo nfi = new
System.Globalization.NumberFormatInfo();
nfi.NumberGroupSeparator = ",";
string s = "14.95";
decimal value = decimal.Parse(s, nfi);
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Ricky Lee" <de****@benzine.com> wrote in message
news:42***********************@news.wanadoo.nl...
Does your system use the . (dot) symbol as the decimal symbol? Some
european
country use the comma (,) as decimal symbol, and dot (.) as the digit
grouping symbol. If you're in the latter case, the conversion will ignore
the dot and treat the "14.95" as "1495". You can solve this by using
specific CultureInfo object to specify the locale that you want to use.

Hope it helps.

-- Ricky Lee
============================================
^o^ "When all doors are closed, God will open a Windows" ^o^
============================================

"BA" <bi***************@gmail.com> wrote in message
news:1109726232.92275607742901ff95e2563b42b197c8@t eranews...

"Peter Mancini" <pe***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
> Show us the code. Copy it from your program. Might be a syntax issue.
>


Thanks for the response, here is the code (cutout try catch for ease of
reading, other funciton just returns the decimal:

>>>>>>>>>>>>>>>code>>>>>>>>>>>>>>>>


public decimal Lookup(string ISBN)
{
string CleanPrice;
decimal decCleanPrice;

System.Diagnostics.Trace.WriteLine("AmazonWebServi ceDLL:AmazonLookupAdapter
ENTER");

//Create Instance of Proxy Class
SService as1 = new SService ();

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchRequest(asin);

CleanPrice =

System.Text.RegularExpressions.Regex.Replace(pi.De tails[0].OurPrice,System.T
ext.RegularExpressions.Regex.Escape("$"),
"");
System.Diagnostics.Trace.WriteLine("price: " + CleanPrice);

decCleanPrice = ConvertStringDecimal(CleanPrice.ToString());

System.Diagnostics.Trace.WriteLine(System.Convert. ToString(decCleanPrice));
System.Diagnostics.Trace.WriteLine("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDecimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert.ToDecimal(stringVal);
System.Diagnostics.Trace.WriteLine("The string as a decimal is {0}.",
decimalVal.ToString());
return decimalVal;

}
catch (System.OverflowException)
{
System.Diagnostics.Trace.WriteLine("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatException)
{
System.Diagnostics.Trace.WriteLine("The string is not formatted as a
decimal.");
return 0;
}
catch (System.ArgumentNullException)
{
System.Diagnostics.Trace.WriteLine("The string is null.");
return 0;
}
}

}
}
>>>>>>>>>>>>>>>code>>>>>>>>>>>>>>>>


Thanks again


Jul 21 '05 #6
What is the locale setting on your machine?

"BA" <bi***************@gmail.com> schrieb im Newsbeitrag
news:1109722721.07f50f5ad92bcdbd43cb46789d9441d5@t eranews...
Hello,

I have a string with a price in: "$14.95"

I need to get it into a decimal.

So I regex 'd the string and dumped the $. Debug mon shows a clean string
"14.95"

Then I do a System.Convert.ToDecimal on the clean string and the output
becomes: 1495

What gives!? I've tried a variety of workarounds but nothing works. How
can I format it for 2 decimal places or perform the convert in a way I am
not aware of?

Thanks alot!

Jul 21 '05 #7
It has to do with the culture of the running thread. See the docs for
System.Threading.Thread.CurrentCulture, System.Globalization.CultureInfo and
System.Globalization.NumberFormatInfo. The property NumberDecimalSeparator
of the class System.Globalization.NumberFormatInfo specifies the decimal
separator to be used for parsing etc.

You can always change the culture for your thread if you wish.

Regards, Jakob.
"BA" wrote:

"Peter Mancini" <pe***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Show us the code. Copy it from your program. Might be a syntax issue.


Thanks for the response, here is the code (cutout try catch for ease of
reading, other funciton just returns the decimal:

>>>>>>>>>>>>code>>>>>>>>>>>>>>>>
public decimal Lookup(string ISBN)
{
string CleanPrice;
decimal decCleanPrice;

System.Diagnostics.Trace.WriteLine("AmazonWebServi ceDLL:AmazonLookupAdapter
ENTER");

//Create Instance of Proxy Class
SService as1 = new SService ();

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchRequest(asin);

CleanPrice =
System.Text.RegularExpressions.Regex.Replace(pi.De tails[0].OurPrice,System.Text.RegularExpressions.Regex.Esc ape("$"),
"");
System.Diagnostics.Trace.WriteLine("price: " + CleanPrice);

decCleanPrice = ConvertStringDecimal(CleanPrice.ToString());

System.Diagnostics.Trace.WriteLine(System.Convert. ToString(decCleanPrice));
System.Diagnostics.Trace.WriteLine("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDecimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert.ToDecimal(stringVal);
System.Diagnostics.Trace.WriteLine("The string as a decimal is {0}.",
decimalVal.ToString());
return decimalVal;

}
catch (System.OverflowException)
{
System.Diagnostics.Trace.WriteLine("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatException)
{
System.Diagnostics.Trace.WriteLine("The string is not formatted as a
decimal.");
return 0;
}
catch (System.ArgumentNullException)
{
System.Diagnostics.Trace.WriteLine("The string is null.");
return 0;
}
}

}
}
>>>>>>>>>>code>>>>>>>>>>>>>>>>


Thanks again

Jul 21 '05 #8

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

Similar topics

4
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got...
7
by: BA | last post by:
Hello, I have a string with a price in: "$14.95" I need to get it into a decimal. So I regex 'd the string and dumped the $. Debug mon shows a clean string "14.95" Then I do a...
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...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
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
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
10
by: TC | last post by:
Hey All, I have strings in currency. For example: "$1.29" What is the best way to convert them to decimal formatted like: "1.29" Thanks, TC
2
by: Lior Bobrov | last post by:
Hi ... How to convert a variable of type Double to String , *preserving* the original value of the Double variable , as is , in a short (convenient) way ? For example , if there are two...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.