473,670 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3024
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.goo glegroups.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.Diagnost ics.Trace.Write Line("AmazonWeb ServiceDLL:Amaz onLookupAdapter
ENTER");

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

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchR equest(asin);

CleanPrice =
System.Text.Reg ularExpressions .Regex.Replace( pi.Details[0].OurPrice,Syste m.Text.RegularE xpressions.Rege x.Escape("$"),
"");
System.Diagnost ics.Trace.Write Line("price: " + CleanPrice);

decCleanPrice = ConvertStringDe cimal(CleanPric e.ToString());

System.Diagnost ics.Trace.Write Line(System.Con vert.ToString(d ecCleanPrice));
System.Diagnost ics.Trace.Write Line("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDe cimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert. ToDecimal(strin gVal);
System.Diagnost ics.Trace.Write Line("The string as a decimal is {0}.",
decimalVal.ToSt ring());
return decimalVal;

}
catch (System.Overflo wException)
{
System.Diagnost ics.Trace.Write Line("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatE xception)
{
System.Diagnost ics.Trace.Write Line("The string is not formatted as a
decimal.");
return 0;
}
catch (System.Argumen tNullException)
{
System.Diagnost ics.Trace.Write Line("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 ConvertStringTo Decimal(string stringVal)
{
try
{
return System.Convert. ToDecimal(strin gVal.TrimStart( '$')); // remove
the "$", convert it to a decimal, then return it
}
catch (Exception ex)
{
System.Diagnost ics.Trace.Write Line(ex.Message );
}
return 0;
}

"BA" <bi************ ***@gmail.com> wrote in message
news:1109726232 .92275607742901 ff95e2563b42b19 7c8@teranews...

"Peter Mancini" <pe***********@ gmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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.Diagnost ics.Trace.Write Line("AmazonWeb ServiceDLL:Amaz onLookupAdapter
ENTER");

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

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchR equest(asin);

CleanPrice =
System.Text.Reg ularExpressions .Regex.Replace( pi.Details[0].OurPrice,Syste m.Text.RegularE xpressions.Rege x.Escape("$"),
"");
System.Diagnost ics.Trace.Write Line("price: " + CleanPrice);

decCleanPrice = ConvertStringDe cimal(CleanPric e.ToString());
System.Diagnost ics.Trace.Write Line(System.Con vert.ToString(d ecCleanPrice));
System.Diagnost ics.Trace.Write Line("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDe cimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert. ToDecimal(strin gVal);
System.Diagnost ics.Trace.Write Line("The string as a decimal is {0}.",
decimalVal.ToSt ring());
return decimalVal;

}
catch (System.Overflo wException)
{
System.Diagnost ics.Trace.Write Line("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatE xception)
{
System.Diagnost ics.Trace.Write Line("The string is not formatted as a
decimal.");
return 0;
}
catch (System.Argumen tNullException)
{
System.Diagnost ics.Trace.Write Line("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 .92275607742901 ff95e2563b42b19 7c8@teranews...

"Peter Mancini" <pe***********@ gmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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.Diagnost ics.Trace.Write Line("AmazonWeb ServiceDLL:Amaz onLookupAdapter ENTER");

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

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchR equest(asin);

CleanPrice =
System.Text.Reg ularExpressions .Regex.Replace( pi.Details[0].OurPrice,Syste m.T
ext.RegularExpr essions.Regex.E scape("$"), "");
System.Diagnost ics.Trace.Write Line("price: " + CleanPrice);

decCleanPrice = ConvertStringDe cimal(CleanPric e.ToString());

System.Diagnost ics.Trace.Write Line(System.Con vert.ToString(d ecCleanPrice)); System.Diagnost ics.Trace.Write Line("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDe cimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert. ToDecimal(strin gVal);
System.Diagnost ics.Trace.Write Line("The string as a decimal is {0}.",
decimalVal.ToSt ring());
return decimalVal;

}
catch (System.Overflo wException)
{
System.Diagnost ics.Trace.Write Line("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatE xception)
{
System.Diagnost ics.Trace.Write Line("The string is not formatted as a
decimal.");
return 0;
}
catch (System.Argumen tNullException)
{
System.Diagnost ics.Trace.Write Line("The string is null.");
return 0;
}
}

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


Thanks again

Jul 21 '05 #5
Try
System.Globaliz ation.NumberFor matInfo nfi = new
System.Globaliz ation.NumberFor matInfo();
nfi.NumberGroup Separator = ",";
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.n l...
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 .92275607742901 ff95e2563b42b19 7c8@teranews...

"Peter Mancini" <pe***********@ gmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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.Diagnost ics.Trace.Write Line("AmazonWeb ServiceDLL:Amaz onLookupAdapter
ENTER");

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

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchR equest(asin);

CleanPrice =

System.Text.Reg ularExpressions .Regex.Replace( pi.Details[0].OurPrice,Syste m.T
ext.RegularExpr essions.Regex.E scape("$"),
"");
System.Diagnost ics.Trace.Write Line("price: " + CleanPrice);

decCleanPrice = ConvertStringDe cimal(CleanPric e.ToString());

System.Diagnost ics.Trace.Write Line(System.Con vert.ToString(d ecCleanPrice));
System.Diagnost ics.Trace.Write Line("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDe cimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert. ToDecimal(strin gVal);
System.Diagnost ics.Trace.Write Line("The string as a decimal is {0}.",
decimalVal.ToSt ring());
return decimalVal;

}
catch (System.Overflo wException)
{
System.Diagnost ics.Trace.Write Line("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatE xception)
{
System.Diagnost ics.Trace.Write Line("The string is not formatted as a
decimal.");
return 0;
}
catch (System.Argumen tNullException)
{
System.Diagnost ics.Trace.Write Line("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 .07f50f5ad92bcd bd43cb46789d944 1d5@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!

Jul 21 '05 #7
It has to do with the culture of the running thread. See the docs for
System.Threadin g.Thread.Curren tCulture, System.Globaliz ation.CultureIn fo and
System.Globaliz ation.NumberFor matInfo. The property NumberDecimalSe parator
of the class System.Globaliz ation.NumberFor matInfo 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.goo glegroups.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.Diagnost ics.Trace.Write Line("AmazonWeb ServiceDLL:Amaz onLookupAdapter
ENTER");

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

SService.lookup = ISBN;

CleanPrice = as1.AsinSearchR equest(asin);

CleanPrice =
System.Text.Reg ularExpressions .Regex.Replace( pi.Details[0].OurPrice,Syste m.Text.RegularE xpressions.Rege x.Escape("$"),
"");
System.Diagnost ics.Trace.Write Line("price: " + CleanPrice);

decCleanPrice = ConvertStringDe cimal(CleanPric e.ToString());

System.Diagnost ics.Trace.Write Line(System.Con vert.ToString(d ecCleanPrice));
System.Diagnost ics.Trace.Write Line("EXIT");

return decCleanPrice;
}

internal decimal ConvertStringDe cimal(string stringVal)
{
decimal decimalVal;

try
{
decimalVal = System.Convert. ToDecimal(strin gVal);
System.Diagnost ics.Trace.Write Line("The string as a decimal is {0}.",
decimalVal.ToSt ring());
return decimalVal;

}
catch (System.Overflo wException)
{
System.Diagnost ics.Trace.Write Line("The conversion from string to
decimal overflowed.");
return 0;
}
catch (System.FormatE xception)
{
System.Diagnost ics.Trace.Write Line("The string is not formatted as a
decimal.");
return 0;
}
catch (System.Argumen tNullException)
{
System.Diagnost ics.Trace.Write Line("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
2502
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 hard time to understand vb syntax. I don't know if anyone in here can point out which is the equivalent object used in c sharp. Translate these two lines to C sharp: Sub Next_Click(Sender As Object, e As EventArgs) Select Case...
7
9500
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 System.Convert.ToDecimal on the clean string and the output
7
6490
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 where I can look up explaination on the String.Format, the format string part like "("{0:.#0}". What's the trailing 0 and what is that "0:" means? I assume the "#" is the place holder character for th value obtain after the comma in the code. ...
6
7607
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, Karthi
5
2315
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
3596
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 starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
10
1235
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
4627
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 variables : Dim dblResult as Double Dim strResult as String
0
8466
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8813
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6212
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1791
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.