Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-02-18
9
Medium Priority
?
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 
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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 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