Solved

Declare a double and show it like 1,0 instead of 1.0.

Posted on 2014-02-18
9
240 Views
Last Modified: 2014-02-18
Hi,

I want to display  the numbers 1.0 and 40.0 as 1,0 and 40,0 because we use the comma instead of the period. So I have put the numbers in a double value. But after testing the code it displayes: 1 instead of 1,0
 
      private void edtVal_InvalidValue(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {
          double Val1 = 1.0;
          double Val2 = 40.0;
            e.ExceptionMode = ExceptionMode.NoAction;
            switch (errType)
            {
                case ErrorType.MOLL:
                    XtraMessageBox.Show("The value must be between " + Val1  + " and " + Val2  "Error");
                    break;
                case ErrorType.MGDL:
                    XtraMessageBox.Show("The value must be between " + 18 + " and " + 720, "Error");
                    break;
                case ErrorType.KG:
                    XtraMessageBox.Show("The value cannot be higher then 400", "Error");
                    break;
                case ErrorType.LBS:
                    XtraMessageBox.Show("The value cannot be higher then 882", "Error");
                    break;
            }
        }

Open in new window


Peter
0
Comment
Question by:peterkiers
  • 5
  • 3
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39867240
to "format" a double to a string message exactly as you need it, you need to do so explicitly:
http://msdn.microsoft.com/en-us/library/kfsatb94%28v=vs.110%29.aspx
0
 
LVL 40
ID: 39867880
Val1.ToString("###.0",new System.Globalization.CultureInfo("fr"))

Open in new window

This forces the display of the decimal part even if it is 0, but using the decimal separator of the French language, which is the coma.

Add more # if you think that you might have values higher than 999.

This being said, I would do that only for stuff that does not display on the screen, such as files that require the coma or reports.

On the screen, you normally never specify a CultureInfo. Your role as a programmer is to follow the Control Panel.

The reason for that is that ToString without a specific culture follows the culture defined in the Control Panel, as do most numeric and date methods in the framework. If you display a coma as a decimal separator on a computer where the language is defined as English, there will be a conflict between what the user sees (the coma) and what he should usually type on that computer (the dot). You will end up with other problems later because the user will try to type a coma and the system won't accept it as a decimal separator.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39867946
I have this:

        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            double val1 = 1.0;
            double val2 = 40.0;
            switch (errType)

            {
                case ErrorType.MOLL:
                    XtraMessageBox.Show("The value must be between " + val1.ToString() + " and " + val2.ToString(), "Error");

Open in new window


still show 1 and 40 in the message.

Peter
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 1

Author Comment

by:peterkiers
ID: 39867968
Val1.ToString("###.0",new System.Globalization.CultureInfo("fr"))

Do I have to specify a country. Is it possible to check the country specified in the pc.

Peter
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39868004
This doesn't work either:

  XtraMessageBox.Show("The value must be between " + val1.ToString("##.#", CultureInfo.CurrentCulture) + " and " + val2.ToString("##.#", CultureInfo.CurrentCulture), "Error");

Open in new window

0
 
LVL 40
ID: 39868032
If you really want a coma, do not specify the country, because you can end up with something else.

On my system for instance, since I am in the Montréal area, I have set my Control Panel to fr-CA for French Canadian. By default, this gives a coma as a decimal separator.

But because I am a programmer and am forced to used a dot in code, it annoys me to have to type a dot in my code and a coma in Excel, so I changed my decimal separator in the Control Panel to a dot even if everything else is fr-CA.

The problem, if I use fr-CA in my code, is that the Framework detects that my Control Panel is fr-CA, but with a dot as a decimal separator, so it adapts to what I have in the Control Panel and uses the dot with fr-CA, while it would normally give a coma.

Setting only for fr solves that problem. You end up with the default for standard French, which is the coma. And standard French cannot be changed through the Control Panel. So you are assured to get a coma.

If you want information about what is defined on the PC, use System.Globalization.CultureInfo.CurrentCulture. This provides you with properties for all the current settings defined in the Control Panel.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39868041
For your last post, use ##.0.

##.# does not force the 0, so when you do not have a decimal value, nothing prints. The 0 forces the output of at least one decimal, putting 0 if there is none.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39868048
Thanks, Peter
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39868089
Ahh, I am happy it works great after testing it:

        private void edtVal_InvalidValue(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            double val1 = 1.0;
            double val2 = 40.0;
            switch (errType)
            {
                case ErrorType.MOLL:
                    XtraMessageBox.Show("The value must be between " + val1.ToString("##.0") + " and " + val2.ToString("##.0"), "Error");

Open in new window


Greetings,

Peter Kiers
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 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