Solved

Change the digits after the decimal point.

Posted on 2011-09-27
3
234 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
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Slimfinger
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:
Slimfinger 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now