473,403 Members | 2,183 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,403 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!


Nov 16 '05 #1
7 9493
Show us the code. Copy it from your program. Might be a syntax issue.

Nov 16 '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
Nov 16 '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

Nov 16 '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

Nov 16 '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


Nov 16 '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!

Nov 16 '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

Nov 16 '05 #8

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

Similar topics

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...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.