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
Solved

Convert.ToDouble with negative number on OS X

Posted on 2011-09-19
8
661 Views
Last Modified: 2012-08-14
Hi,

In our Silverlight applicaiton when we uses Convert.ToDouble("-1") on an OS X (10.7) it crashes with:
"Unhandled Error in Silverlight 4 Application [Format_InvalidString]"

Any negative values will crash.

Even if we run a virtual Windows on the mac it will crash with the same error message. On a clean Windows machine it works fine.

Any help appreciated.
0
Comment
Question by:xlmaster200
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36559281
try to cast if convert not works well.

(Double)"-1";
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36559282
Avoid exception using TryParse method.

if(Double.TryParse("-1") != null )
   Convert.ToDouble("-1");

0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36559291
Try to use Double.Parse("-1") then.

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:xlmaster200
ID: 36559506
I could try to cast it but I'd really liked to now if anyone can explain why it's not working, is it a bug in the runtime?


0
 
LVL 3

Accepted Solution

by:
smickle earned 500 total points
ID: 36565299
I've had similar issues in the past. May not even be isolated to the Mac, but in my case it was the default Culture of the machine that was making stuff explode.

By default, .NET will use the default culture of the machine that is spinning up the runtime, unless you explicity specify which culture to use in your code or configurations. In order for the runtime to know "how" to convert something from some string to a typed double, it uses a format string to determine the conversion pattern.

I ended up having to do stuff like this:
 
// use InvariantCulture
var someDouble = Double.Parse("1234.0",CultureInfo.InvariantCulture);

Open in new window


By using InvariantCulture, the runtime didn't have to use the machine's default culture and possibly change it's conversion pattern. In my code snipped above, the dot in 1234.0 was getting interpretted as a thousands separator in a different culture. So I got the weird FormatException.

This is just a guess as a possibility of why it is happening.

In my case, I was pulling my hair out thinking is was something to do with the difference between a 32-bit system and a 64-bit system. It was just coincidental that the OS's were different, so that lead me down way too many wrong turns to a solution.

Hope this helps.

-Steve
0
 

Author Comment

by:xlmaster200
ID: 36565904
Thanks for your answer Steve.
So in other words I can't use Convert.ToDouble (or Convert.ToInt16) anywhere in the code...
I did some test on the client side for a Mac:
string s = "-1";
double d =Convert.ToDouble(s)   - Unhandled Error in Silverlight 4 Application [Format_InvalidString]
double d = Double.Parse(s)  - Unhandled Error in Silverlight 4 Application [Format_InvalidString]
double d = Double.Parse(s,CultureInfo.InvariantCulture) - OK

What culture does not accept "-" before a number? If I try cdbl("-1") in a VBA macro on the Mac it works OK.

It feels no good to change Convert.ToDouble everywhere in the code.
0
 

Author Comment

by:xlmaster200
ID: 36566598
I printed out CultureInfo.CurrentCulture.NumberFormat.NegativeSign and found that on the Mac the NegativeSign was a little longer than in Windows... Silly me not thinking of that...

So I go with your suggestion Steve and use InvariantCulture on the client side
0
 
LVL 3

Expert Comment

by:smickle
ID: 36570840
Ha, extra long negative sign. That's great.

You might be able to get around changing all the code, and you can explicitly set your desired culture in web.config, if this is an asp.net app. Not sure if this will work off the top of my head, but it could. This would ensure that all your conversions in your code that run within your web app will use a preset culture.

 
<!-- system.web section inside your web.config -->
<globalization culture="en-US"/>

Open in new window


Globalization can be a headache in many sneaky ways though. Especially if you have values stored and retrieved from SQL Server for example. And the SQL Server has a different culture setting than the code that uses it. They must match in order to get predictable results. DateTime formats and conversions are the main ones that get hosed in that scenario due to the different order of the mm/dd/yy structure.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

If you other experts are anything like me you are always looking into and testing out new features. While I was doing some research one day I ran across an app that I installed on my Mac and used as a security system. Mac OS X: SecureHome uses your …
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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