Solved

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

Posted on 2014-02-18
9
231 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:peterkiers
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, Peter
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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…

744 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