Solved

Change the digits after the decimal point.

Posted on 2011-09-27
3
239 Views
Last Modified: 2012-05-12

I have a "Price" field that contains values like : 50.22, 8.78, 68.19, etc.

I want the last 2 digits after the decimal point to be rounded like this :

If :  .01 to .29  =  .29

If :  .30 to .49  =  .49

If :  .50 to .79  =  .79

If :  .79 to .99  =  .95

So if Price is "29.14", it would be "29.29", . . . "18.78" would be "18.99", etc.  thanks

How can I do this in C#?   thanks.
0
Comment
Question by:MikeMCSD
[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
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Bill Nolan
ID: 36712699
There may be a slicker way to do this, but it's simple enough like this:

decimal valOriginal = 19.77m;
decimal valLeft = ((double)((int)valOriginal);
decimal valRight = valOriginal - valLeft;

decimal valOutput = valLeft +
   (valRight < 0.30m ? 0.29m :
   (valRight < 0.50m ? 0.49m :
   (valRight < 0.80m ? 0.79m : 0.95m)));
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 36712898
thanks slim . .  

Here is it using SQL :

                        SET Price =
                        CASE WHEN Price-floor(Price) >= 0.70 THEN floor(Price) + 0.95 ELSE
                        CASE WHEN Price-floor(Price) >= 0.50 THEN floor(Price) + 0.69 ELSE
                        CASE WHEN Price-floor(Price) >= 0.30 THEN floor(Price) + 0.49 ELSE
                        CASE WHEN Price-floor(Price) >= 0 THEN floor(Price) + 0.29
                        END END END END


Is there a similar function for "floor" in C#?  I can't remember what floor does anyway.

I would like to use a similar format like this because it's easier to understand.

0
 
LVL 7

Accepted Solution

by:
Bill Nolan earned 500 total points
ID: 36712947
They are implemented in the System.Math class.

I believe they typically do this:

floor(2.77) = 2.0
ceiling(2.77) = 3.0

0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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