truncating decimal points in c# - (Wednesday, September 30, 2009)
Ran into an interesting problem today.
I need to truncate a number to 2 decimal places, which basically means chopping off the extra digits.
Eg:
2.919 -> 2.91
2.91111 -> 2.91
Why? This is what SQL server is doing when storing a number of a particular precision. Eg, if a column is Decimal(8,2), (TxnComponent.AdjustedCost), and you try to insert/update a number of 9.1234, the 3 and 4 will be chopped off.
I need to do it in c# code to mimick what the database is doing.
As far as I can work out there's no library available to do it!
The only possible ways of doing it are either:
1. Using the stringformatter to "print" it out only two decimal places, and then casting it to a decimal,
eg:
decimal longDecimal = 2.1345
decimal shorterDecimal = Convert.ToDecimal(longDecimal.ToString("0.##"));
// shorterDecimal is now 2.13
I'm not happy with this because it involves a to-string and then another string to decimal conversion. Ridiculous!
2. Using Math.Truncate (which only accepts an integer), so I can multiply it by 100, truncate it, then divide by 100. eg:
decimal longDecimal = 2.1235;
decimal shortDecimal = Math.Truncate(longDecimal * 100) / 100;
I'm also not happy with this because if longDecimal is 0, I'll get a divide by 0 error. Ridiculous.
Surely there's a better + easier way!! Any suggestions?
Direct Link Tags: truncating decimal points in c#
Date: Thursday, October 01, 2009 15:16
User: Levi
Comment:
decimal shortDecimal = Math.Truncate(0 * 100) / 100;
I fail to see the divide by zero?
Date: Thursday, October 01, 2009 21:13
User: lach
Comment: Yeah I know. I realised that. I'm a git. It's still a bodgy solution, but I guess it works.
That's it for this article - you can either go back to the List of posts page, or perhaps the MetalTheater.com front page.
MetalTheater.com
video guitar lessons, musician interviews, reviews of instrumental rock + progressive metal!
Guitar Video Lessons
- - Finger Tapping - part 1
- - Finger Tapping - part 2
- - Effects P1: Distortion, Gain, Fuzz, Crunch and Overdrive
- - How to play 'Stream Of Consciousness' by Dream Theater
Guitar Playing Videos
- - 'More Than a Dream'
- - Live Guitar Solo
- - 'King Of The Celery People'
- - Greg Howe 'Play for me' Comp
Band Video Clips
- - Leap Of Faith (awesome!)
- - Angry Videogame Nerd Cover
- - More Than a Dream
- Video Player (all vids)
Interviews
- - Brett Garsed
- - DevilDriver
- - James LaBrie
- - Machine Head
- - Megadeth
- - Trivium
- - Vanishing Point
Instrumental Rock Reviews
Progressive Reviews
- - Chroma Key
- - Dalis Dilemma
- - Dream Theater
- - Eternity X
- - Event
- - Flower Kings
- - Galactic Cowboys
- - Iron Maiden
- - Loudness
- - Manowar
- - MullMuzzler
- - OSI
- - Pain Of Salvation
- - Rage
- - Rhapsody Of Fire
- - Spastic Ink
- - Vanishing Point
Other Reviews
- - Incubus
- - KMFDM
- - Korn
- - Living Colour
- - Yankovic, Weird Al
Recommended Sites
- - truthinshredding.blogspot.com
- - pyromusic.net
- - progarchives.com
- - dws movie buffs
- - chris m photos
- - nelle of the jungle
- - Goodridge Green
Other Stuff
- - Dark Castle
- - RSS Feed
- - My Music
- - My Vids
- - My Cache Finds
- - SiteMap
(C)opyright 2006 Metaltheater.com







