Solved

Currency, doubles, points and comma's

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now