Solved

Change the digits after the decimal point.

Posted on 2011-09-27
3
240 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

705 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