?
Solved

Change the digits after the decimal point.

Posted on 2011-09-27
3
Medium Priority
?
241 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 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