Solved

Change the digits after the decimal point.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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