Results 1 to 5 of 5

Thread: Round up program for C#

  1. #1
    Join Date
    Jul 2006
    Posts
    191

    Round up program for C#

    Hi, I want help for rounding the decimals with the nearest Integer. Consider the following code :
    public test(int x, int y)
    {
    . . .

    int scaled = x / y;

    . . .
    }
    If the result of the operation x / y is a fraction, I want to round up to the nearest integer in C#. Suppose the result of x / y is 4.57 and want that convert to 5. Please help me solving my query..!!
    ASUS P5VD1-X
    Intel Pentium Dual Core 3.00GHz
    Maxtor 160GB
    Corsair 1.5GB PC3200 RAM
    Nvidia Geforce 6800 GT 256mb
    Phillips DVD RW
    Magna 500W PSU
    XION II Steel Black Gaming Case

  2. #2
    Join Date
    Mar 2008
    Posts
    672

    Re: Round up program for C#

    I think that the following coding can help you to round up program in C#. Here is the code for that :
    Code:
    using System;
    using System.Data;
    using System.Text.RegularExpressions;
    
    class Class1{
            static void Main(string[] args){
          Console.WriteLine(RoundUp(.7));
          Console.WriteLine(RoundUp(.8));
          Console.WriteLine(RoundUp(.9));
          Console.WriteLine(RoundUp(1.7));
          Console.WriteLine(RoundUp(1.8));
          Console.WriteLine(RoundUp(1.9));
          Console.WriteLine(RoundUp(2.7));
          Console.WriteLine(RoundUp(2.8));
          Console.WriteLine(RoundUp(2.9));
          Console.WriteLine(RoundUp(3.7));
          Console.WriteLine(RoundUp(3.8));
          Console.WriteLine(RoundUp(3.9));
            }
        public static double RoundUp(double valueToRound)
        {
          return (Math.Floor(valueToRound + 0.5));
        }
    
    }

  3. #3
    Join Date
    Mar 2008
    Posts
    349

    Re: Round up program for C#

    Have you tried to use the System.Math.Ceiling? By using the System.Math.Ceiling, it will return the smallest whole number greater than or equal to the specified number. If you are putting some negative numbers, then it will return numbers closer to zero.
    For EG : Ceiling(-3.6) will return -3.
    Also try to use the function System.Match.Floor. The System.Match.Floor will return the smallest whole number less than or equal to the specified number. Hope that it will help you in rounding the fraction to an Integer in your code.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Round up program for C#

    You can use the following code in PHP for rounding the numbers to the nearest integer, and here is the code for that :
    Code:
    echo "round 15.545 -".round(15.545,2)."<br>";
    echo "round 15.555 -".round(15.555,2)."<br>";
    echo "number_format 15.545 -".number_format(15.545, 2, ',',
    '.')."<br>";
    echo "number_format 15.555 -".number_format(15.555, 2, ',',
    '.')."<br>";

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: Round up program for C#

    I think that using the System.Math.Ceiling will help you. I was knowing C# .Net Round Up method with precision selection. And hence I thought to provide the code for the same. Here is the code for Round Up method with precision selection in C# .Net :
    Code:
    public static double RoundUp(double figure, int precision)
    {
        double newFigure = Math.Round(figure, precision);
        double difference = figure - newFigure;
        double tolerance = 1 / Math.Pow(10, precision + 1);
        if (difference > tolerance) //Figure was rounded down
        {
            double padding = 1 / Math.Pow(10, precision);
            newFigure += padding;
        }
        return newFigure;
    }

Similar Threads

  1. Another round of waterblocks on i7
    By Galimberti in forum Overclocking & Computer Modification
    Replies: 5
    Last Post: 01-09-2010, 04:37 AM
  2. How to use QQ round blocking software
    By Kalanidhi in forum Technology & Internet
    Replies: 4
    Last Post: 07-03-2010, 03:46 AM
  3. Round a number in VB-Script
    By Jesus-Ernesto in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 11:42 AM
  4. Need help on ROUND function in excel
    By Cade in forum Windows Software
    Replies: 5
    Last Post: 26-12-2009, 08:29 PM
  5. Round Portable DVD Player
    By AdamT in forum Portable Devices
    Replies: 0
    Last Post: 12-07-2008, 12:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,009,015.94654 seconds with 17 queries