Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
(Sort)Rounding Magic - C#
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 /* Rounding functions to Round a decimal number to nearest 0.5, 0.25, 2 These can be used in Ranking or Rating system Example: 1.0 should be equal to 1. Similarly, 1.1 = 1, 1.2 = 1, 1.3 = 1.5, 1.4 = 1.5... 1.8 = 2 and so on while Rounding to nearest one-half (0.5) */ using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { for(decimal i=1;i<=2;i+=0.1m) Console.WriteLine("{0}, NearestOneHalf: {1}",i,NearestOneHalf(i)); Console.WriteLine(""); for(decimal i=1;i<=3;i+=0.1m) Console.WriteLine("{0}, NearestTwo: {1}",i,NearestTwo(i)); Console.WriteLine(""); for(decimal i=1;i<=3;i+=0.1m) Console.WriteLine("{0}, Nearest Quarter: {1}",i,NearestQuarter(i)); } //Round to nearest 0.5 public static decimal NearestOneHalf(decimal d) { decimal d1 = d*2; decimal d2 = Math.Round(d1,MidpointRounding.AwayFromZero); decimal d3 = d2/2; return d3; } //Round to a number nearest to a multiple of 2 public static decimal NearestTwo(decimal d) { decimal d1 = (d/2)*2; decimal d2 = Math.Round(d1,MidpointRounding.AwayFromZero); return d2; } //Round to a number nearest to Quarter .25 public static decimal NearestQuarter(decimal d) { decimal d1 = d*4; decimal d2 = Math.Round(d1,MidpointRounding.AwayFromZero); decimal d3 = d2/4; return d3; } } }
run
|
edit
|
history
|
help
0
max & min binary search tree
466
https://www.fxp.co.il/showthread.php?t=21125499
Enigme
Punnet Square Generator
test_hash
show the date
name generator
aaa
IEquatable doubt resolved!!