PC Review


Reply
Thread Tools Rate Thread

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

 
 
BA
Guest
Posts: n/a
 
      2nd Mar 2005
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!




 
Reply With Quote
 
 
 
 
Peter Mancini
Guest
Posts: n/a
 
      2nd Mar 2005
Show us the code. Copy it from your program. Might be a syntax issue.

 
Reply With Quote
 
BA
Guest
Posts: n/a
 
      2nd Mar 2005

"Peter Mancini" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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("AmazonWebServiceDLL: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.Details[0].OurPrice,System.Text.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


 
Reply With Quote
 
SB
Guest
Posts: n/a
 
      2nd Mar 2005
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" <(E-Mail Removed)> wrote in message
news:1109726232.92275607742901ff95e2563b42b197c8@teranews...
>
> "Peter Mancini" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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("AmazonWebServiceDLL: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.Details[0].OurPrice,System.Text.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
>



 
Reply With Quote
 
Ricky Lee
Guest
Posts: n/a
 
      2nd Mar 2005
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" <(E-Mail Removed)> wrote in message
news:1109726232.92275607742901ff95e2563b42b197c8@teranews...
>
> "Peter Mancini" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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("AmazonWebServiceDLL: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.Details[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
>
>



 
Reply With Quote
 
cody
Guest
Posts: n/a
 
      2nd Mar 2005
What is the locale setting on your machine?

"BA" <(E-Mail Removed)> schrieb im Newsbeitrag
news:1109722721.07f50f5ad92bcdbd43cb46789d9441d5@teranews...
> 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!
>
>
>
>



 
Reply With Quote
 
Dennis Myrén
Guest
Posts: n/a
 
      2nd Mar 2005
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" <(E-Mail Removed)> wrote in message
news:422528df$0$49370$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:1109726232.92275607742901ff95e2563b42b197c8@teranews...
>>
>> "Peter Mancini" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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("AmazonWebServiceDLL: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.Details[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
>>
>>

>
>



 
Reply With Quote
 
=?Utf-8?B?SmFrb2IgQ2hyaXN0ZW5zZW4=?=
Guest
Posts: n/a
 
      2nd Mar 2005
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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("AmazonWebServiceDLL: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.Details[0].OurPrice,System.Text.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
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting 2-place decimal value to floating point decimal number with leading zero Kermit Piper Microsoft Excel Misc 3 18th Mar 2006 06:20 PM
FIXED 2 DECIMAL PLACES, MUST ENTER ALL ZEROES AFTER DECIMAL POINT. =?Utf-8?B?U1VLWUtJVFRZ?= Microsoft Excel Misc 3 6th Jul 2005 01:50 PM
setting the decimal variable to include 2 numbers after decimal point? Scott Microsoft VB .NET 1 3rd Mar 2005 10:05 PM
C# - C sharp - String to decimal, decimal point disappears! BA Microsoft C# .NET 7 2nd Mar 2005 08:43 AM
Decimal class now preserves trailing zeroes after the decimal point Uncle Goh Microsoft Dot NET Framework 0 11th Sep 2003 09:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:25 AM.