-  Login
Current Page:  Home
 Search
Quick Links
  
My Flickr
     
 

No, I'm not that Bryan Robson...

Welcome to the latest incarnation of BryanRobson.net. The content from the old DotNetNuke portal has been transferred, and can be found through the menus above.

Since most visitors (well, the intentional ones, rather than the footy fans) are here for my Smartphone software, Sudoku and Blackjack for Windows Mobile 2003 devices, direct links for these are on the left.


Below you can also find the content of my last blog posting, and links to previous posts.

Breaking News:We're ranked #1 on Ask.com for "obscenely comfy seating"! I'm so proud...

  
Recent blog posts
Decimals, VB.Net, XML, and decimal places!

Thanks to a colleague who I shall refer to only as "Mr. Picky", I've just been playing with decimals.

I know they're the slowest numeric data type, but we're talking XML and xs:decimal, and classes created thereof.

Mr. Picky's complaint was that of all of the values (prices, in fact) in the XML file, some were formatted with 2 decimal places, and some with none, and he'd like to see them all with 2.

Now, after a bit of fiddling about, I couldn't see any obvious reason why we had these differences.  The prices all originated in the same fields in the database, and they all ended up as XML attributes declared as xs:decimal, and passed through VB.Net classes as Decimal variables.

The heart of the issue is VB.Net's Decimal handling.  By default a Decimal has zero decimal places, but if it acquires any along the way then it will keep them.  Some of my values had spent part of their life as a string (something like "£12.34", and don't ask why!) and some were just plain Decimals.  All the attributes where the values had been strings had their 2DP, as they'd been parsed from strings like "12.00" and so when the VB.Net class went ToXml() they appeared as "12.00", but those that were never strings stayed as "12".

My solution?  (and I ain't proud of this)

Dim decimalWith2DecimalPlaces As Decimal = decimalValueFromDb * Decimal.Parse("1.00")

...and I can't find any better way of doing this.  I've spent frankly far too long on a trivial bug as it is, and I've got plenty more to be working on, so that's that!

EDIT: Ignore me - if you do that you can end up with this: "12.300", which is clearly nonsense. I've abandoned all pride and started doing this instead:

Dim decimalWith2DecimalPlaces As Decimal = Decimal.Parse(decimalValueFromDb.ToString("0.00"))



Ten people I don’t know but like sharing the planet with
The Quackometer: Is Chiropractic X-raying Illegal?
Fear of a fierce feline
Ethernet over Powerline (Netgear HDXB101)
She & Him, live @ Newport
Calexico: Two Silver Trees (mp3)
Controversy
Booze Test
Weekend of music
Ignore this post...
ALT.NET Geek Code Generator
 
  
Privacy Statement  |  Terms Of Use  |  Copyright 2007 by Bryan Robson