Solved

Currency, doubles, points and comma's

Posted on 2003-12-02
3
600 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 125 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
Video Player 2017 5 24
SqlDataBase 7 48
Hey!! 5 33
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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