Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Currency, doubles, points and comma's

Posted on 2003-12-02
3
Medium Priority
?
609 Views
Last Modified: 2008-02-01
Hello,

My problem:

I have difficulties with prizes in my database to convert and display correctly.
I'm using a MySQL database, the field of the prizes is set to varchar(255) (don't ask me why, I didn't design it) and the prizes are stored with a point not a comma.

I try to convert a prize like "12.50" using Convert.ToDouble(). However, if I try display this result it gives me '125'. So it leaves the point, AND the zero.

I want everything to displayed as 12,50. And I must be able to do some necessary calculations with the database result as well. What is the best way to do this? I know it could probably be done by replacing the point in the string to a comma and convert everything to double and then do some necessary calculations, and then convert it back to string so I can replace the point again for the comma... or something like that... but I don't like that obviously.

In java you have some Currency format or something. I'm sure in C# it can be done by using some sort of IFormatter or Culture settings, but I know nothing about them. So, what is the best way to do this without touching the database format?

Thanks!

Razzie
0
Comment
Question by:Razzie_
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
testn earned 500 total points
ID: 9858289
You can set CurrencyDecimalSeparator and CurrencyGroupSeparator
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationnumberformatinfoclasscurrencydecimaldigitstopic.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationnumberformatinfoclasscurrencydecimaldigitstopic.asp

If you want to make it all over the application, you can set the CurrentCulture....

CultureInfo cul =  new CultureInfo( "en-US", false );
NumberFormatInfo nfi = cul.NumberFormat;
nfi.CurrencyDecimalSeparator = ",";

Thread.CurrentThread.CurrentCulture = cul;
0
 
LVL 18

Expert Comment

by:testn
ID: 9858301
Also you have to use the correct overloading function to convert string to double.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemConvertClassToDoubleTopic10.asp
0
 
LVL 8

Author Comment

by:Razzie_
ID: 9858882
CultureInfo cul =  new CultureInfo( "en-US", false );
NumberFormatInfo nfi = cul.NumberFormat;
nfi.CurrencyDecimalSeparator = ",";

Thread.CurrentThread.CurrentCulture = cul;

---------------------------------------------------------------

Exactly what I was looking for, works perfectly! Thank you!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question