[{"data":1,"prerenderedAt":771},["ShallowReactive",2],{"blog:2006:dotnettipsuptimeagerounding":3,"blogMore-Development":757,"comments-dotnettipsuptimeagerounding":770},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":15,"body":34,"_type":748,"_id":749,"_source":750,"_file":751,"_stem":752,"_extension":753,"url":754,"wordCount":755,"minutes":63,"commentCount":756},"/blog/2006/dotnettipsuptimeagerounding","2006",false,"en",".NET quick samples: Up-times, ages, rounding to n places","Just a few quick .NET samples for performing some common tasks that the .NET Framework doesn’t do for you:","2006-10-20T14:39:42+00:00","Development",[13,14],".NET","C#",{"type":16,"children":17},"root",[18,25],{"type":19,"tag":20,"props":21,"children":22},"element","p",{},[23],{"type":24,"value":9},"text",{"type":19,"tag":20,"props":26,"children":27},{},[28],{"type":19,"tag":29,"props":30,"children":31},"em",{},[32],{"type":24,"value":33},"[)amien",{"type":16,"children":35,"toc":743},[36,40,47,271,277,520,526,730,737],{"type":19,"tag":20,"props":37,"children":38},{},[39],{"type":24,"value":9},{"type":19,"tag":41,"props":42,"children":44},"h2",{"id":43},"system-uptime",[45],{"type":24,"value":46},"System uptime",{"type":19,"tag":48,"props":49,"children":54},"pre",{"className":50,"code":51,"language":52,"meta":53,"style":53},"language-csharp shiki shiki-themes everforest-light dracula","using System.Diagnostics;\n\npublic TimeSpan GetUptime() {\n    var systemUpTime = new PerformanceCounter(\"System\", \"System Up Time\");\n    systemUpTime.NextValue(); // Required to work!\n    return TimeSpan.FromSeconds(systemUpTime.NextValue())));\n}\n","csharp","",[55],{"type":19,"tag":56,"props":57,"children":58},"code",{"__ignoreMap":53},[59,93,103,130,204,229,262],{"type":19,"tag":60,"props":61,"children":64},"span",{"class":62,"line":63},"line",1,[65,71,77,83,88],{"type":19,"tag":60,"props":66,"children":68},{"style":67},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[69],{"type":24,"value":70},"using",{"type":19,"tag":60,"props":72,"children":74},{"style":73},"--shiki-default:#DF69BA;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[75],{"type":24,"value":76}," System",{"type":19,"tag":60,"props":78,"children":80},{"style":79},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[81],{"type":24,"value":82},".",{"type":19,"tag":60,"props":84,"children":85},{"style":73},[86],{"type":24,"value":87},"Diagnostics",{"type":19,"tag":60,"props":89,"children":90},{"style":79},[91],{"type":24,"value":92},";\n",{"type":19,"tag":60,"props":94,"children":96},{"class":62,"line":95},2,[97],{"type":19,"tag":60,"props":98,"children":100},{"emptyLinePlaceholder":99},true,[101],{"type":24,"value":102},"\n",{"type":19,"tag":60,"props":104,"children":106},{"class":62,"line":105},3,[107,113,119,125],{"type":19,"tag":60,"props":108,"children":110},{"style":109},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[111],{"type":24,"value":112},"public",{"type":19,"tag":60,"props":114,"children":116},{"style":115},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[117],{"type":24,"value":118}," TimeSpan",{"type":19,"tag":60,"props":120,"children":122},{"style":121},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[123],{"type":24,"value":124}," GetUptime",{"type":19,"tag":60,"props":126,"children":127},{"style":79},[128],{"type":24,"value":129},"() {\n",{"type":19,"tag":60,"props":131,"children":133},{"class":62,"line":132},4,[134,140,145,150,155,160,165,171,177,181,186,190,195,199],{"type":19,"tag":60,"props":135,"children":137},{"style":136},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[138],{"type":24,"value":139},"    var",{"type":19,"tag":60,"props":141,"children":142},{"style":79},[143],{"type":24,"value":144}," systemUpTime ",{"type":19,"tag":60,"props":146,"children":147},{"style":109},[148],{"type":24,"value":149},"=",{"type":19,"tag":60,"props":151,"children":152},{"style":67},[153],{"type":24,"value":154}," new",{"type":19,"tag":60,"props":156,"children":157},{"style":115},[158],{"type":24,"value":159}," PerformanceCounter",{"type":19,"tag":60,"props":161,"children":162},{"style":79},[163],{"type":24,"value":164},"(",{"type":19,"tag":60,"props":166,"children":168},{"style":167},"--shiki-default:#8DA101;--shiki-dark:#E9F284",[169],{"type":24,"value":170},"\"",{"type":19,"tag":60,"props":172,"children":174},{"style":173},"--shiki-default:#8DA101;--shiki-dark:#F1FA8C",[175],{"type":24,"value":176},"System",{"type":19,"tag":60,"props":178,"children":179},{"style":167},[180],{"type":24,"value":170},{"type":19,"tag":60,"props":182,"children":183},{"style":79},[184],{"type":24,"value":185},", ",{"type":19,"tag":60,"props":187,"children":188},{"style":167},[189],{"type":24,"value":170},{"type":19,"tag":60,"props":191,"children":192},{"style":173},[193],{"type":24,"value":194},"System Up Time",{"type":19,"tag":60,"props":196,"children":197},{"style":167},[198],{"type":24,"value":170},{"type":19,"tag":60,"props":200,"children":201},{"style":79},[202],{"type":24,"value":203},");\n",{"type":19,"tag":60,"props":205,"children":207},{"class":62,"line":206},5,[208,213,218,223],{"type":19,"tag":60,"props":209,"children":210},{"style":79},[211],{"type":24,"value":212},"    systemUpTime.",{"type":19,"tag":60,"props":214,"children":215},{"style":121},[216],{"type":24,"value":217},"NextValue",{"type":19,"tag":60,"props":219,"children":220},{"style":79},[221],{"type":24,"value":222},"(); ",{"type":19,"tag":60,"props":224,"children":226},{"style":225},"--shiki-default:#939F91;--shiki-default-font-style:italic;--shiki-dark:#6272A4;--shiki-dark-font-style:inherit",[227],{"type":24,"value":228},"// Required to work!\n",{"type":19,"tag":60,"props":230,"children":232},{"class":62,"line":231},6,[233,238,243,248,253,257],{"type":19,"tag":60,"props":234,"children":235},{"style":67},[236],{"type":24,"value":237},"    return",{"type":19,"tag":60,"props":239,"children":240},{"style":79},[241],{"type":24,"value":242}," TimeSpan.",{"type":19,"tag":60,"props":244,"children":245},{"style":121},[246],{"type":24,"value":247},"FromSeconds",{"type":19,"tag":60,"props":249,"children":250},{"style":79},[251],{"type":24,"value":252},"(systemUpTime.",{"type":19,"tag":60,"props":254,"children":255},{"style":121},[256],{"type":24,"value":217},{"type":19,"tag":60,"props":258,"children":259},{"style":79},[260],{"type":24,"value":261},"())));\n",{"type":19,"tag":60,"props":263,"children":265},{"class":62,"line":264},7,[266],{"type":19,"tag":60,"props":267,"children":268},{"style":79},[269],{"type":24,"value":270},"}\n",{"type":19,"tag":41,"props":272,"children":274},{"id":273},"calculating-age",[275],{"type":24,"value":276},"Calculating age",{"type":19,"tag":48,"props":278,"children":280},{"className":50,"code":279,"language":52,"meta":53,"style":53},"public int GetAge(DateTime birthday) { \n  return DateTime.Now.Year - birthday.Year -\n      (((DateTime.Now.Month \u003C birthday.Month) ||\n      (DateTime.Now.Month == birthday.Month &&\n      DateTime.Now.Day \u003C birthday.Day)) ? 1 : 0);\n}\n",[281],{"type":19,"tag":56,"props":282,"children":283},{"__ignoreMap":53},[284,321,368,412,450,513],{"type":19,"tag":60,"props":285,"children":286},{"class":62,"line":63},[287,291,296,301,305,310,316],{"type":19,"tag":60,"props":288,"children":289},{"style":109},[290],{"type":24,"value":112},{"type":19,"tag":60,"props":292,"children":293},{"style":136},[294],{"type":24,"value":295}," int",{"type":19,"tag":60,"props":297,"children":298},{"style":121},[299],{"type":24,"value":300}," GetAge",{"type":19,"tag":60,"props":302,"children":303},{"style":79},[304],{"type":24,"value":164},{"type":19,"tag":60,"props":306,"children":307},{"style":115},[308],{"type":24,"value":309},"DateTime",{"type":19,"tag":60,"props":311,"children":313},{"style":312},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[314],{"type":24,"value":315}," birthday",{"type":19,"tag":60,"props":317,"children":318},{"style":79},[319],{"type":24,"value":320},") { \n",{"type":19,"tag":60,"props":322,"children":323},{"class":62,"line":95},[324,329,334,340,344,349,354,359,363],{"type":19,"tag":60,"props":325,"children":326},{"style":67},[327],{"type":24,"value":328},"  return",{"type":19,"tag":60,"props":330,"children":331},{"style":79},[332],{"type":24,"value":333}," DateTime.",{"type":19,"tag":60,"props":335,"children":337},{"style":336},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[338],{"type":24,"value":339},"Now",{"type":19,"tag":60,"props":341,"children":342},{"style":79},[343],{"type":24,"value":82},{"type":19,"tag":60,"props":345,"children":346},{"style":336},[347],{"type":24,"value":348},"Year",{"type":19,"tag":60,"props":350,"children":351},{"style":109},[352],{"type":24,"value":353}," -",{"type":19,"tag":60,"props":355,"children":356},{"style":79},[357],{"type":24,"value":358}," birthday.",{"type":19,"tag":60,"props":360,"children":361},{"style":336},[362],{"type":24,"value":348},{"type":19,"tag":60,"props":364,"children":365},{"style":109},[366],{"type":24,"value":367}," -\n",{"type":19,"tag":60,"props":369,"children":370},{"class":62,"line":105},[371,376,380,384,389,394,398,402,407],{"type":19,"tag":60,"props":372,"children":373},{"style":79},[374],{"type":24,"value":375},"      (((DateTime.",{"type":19,"tag":60,"props":377,"children":378},{"style":336},[379],{"type":24,"value":339},{"type":19,"tag":60,"props":381,"children":382},{"style":79},[383],{"type":24,"value":82},{"type":19,"tag":60,"props":385,"children":386},{"style":336},[387],{"type":24,"value":388},"Month",{"type":19,"tag":60,"props":390,"children":391},{"style":109},[392],{"type":24,"value":393}," \u003C",{"type":19,"tag":60,"props":395,"children":396},{"style":79},[397],{"type":24,"value":358},{"type":19,"tag":60,"props":399,"children":400},{"style":336},[401],{"type":24,"value":388},{"type":19,"tag":60,"props":403,"children":404},{"style":79},[405],{"type":24,"value":406},") ",{"type":19,"tag":60,"props":408,"children":409},{"style":109},[410],{"type":24,"value":411},"||\n",{"type":19,"tag":60,"props":413,"children":414},{"class":62,"line":132},[415,420,424,428,432,437,441,445],{"type":19,"tag":60,"props":416,"children":417},{"style":79},[418],{"type":24,"value":419},"      (DateTime.",{"type":19,"tag":60,"props":421,"children":422},{"style":336},[423],{"type":24,"value":339},{"type":19,"tag":60,"props":425,"children":426},{"style":79},[427],{"type":24,"value":82},{"type":19,"tag":60,"props":429,"children":430},{"style":336},[431],{"type":24,"value":388},{"type":19,"tag":60,"props":433,"children":434},{"style":109},[435],{"type":24,"value":436}," ==",{"type":19,"tag":60,"props":438,"children":439},{"style":79},[440],{"type":24,"value":358},{"type":19,"tag":60,"props":442,"children":443},{"style":336},[444],{"type":24,"value":388},{"type":19,"tag":60,"props":446,"children":447},{"style":109},[448],{"type":24,"value":449}," &&\n",{"type":19,"tag":60,"props":451,"children":452},{"class":62,"line":206},[453,458,462,466,471,475,479,483,488,493,499,504,509],{"type":19,"tag":60,"props":454,"children":455},{"style":79},[456],{"type":24,"value":457},"      DateTime.",{"type":19,"tag":60,"props":459,"children":460},{"style":336},[461],{"type":24,"value":339},{"type":19,"tag":60,"props":463,"children":464},{"style":79},[465],{"type":24,"value":82},{"type":19,"tag":60,"props":467,"children":468},{"style":336},[469],{"type":24,"value":470},"Day",{"type":19,"tag":60,"props":472,"children":473},{"style":109},[474],{"type":24,"value":393},{"type":19,"tag":60,"props":476,"children":477},{"style":79},[478],{"type":24,"value":358},{"type":19,"tag":60,"props":480,"children":481},{"style":336},[482],{"type":24,"value":470},{"type":19,"tag":60,"props":484,"children":485},{"style":79},[486],{"type":24,"value":487},")) ",{"type":19,"tag":60,"props":489,"children":490},{"style":109},[491],{"type":24,"value":492},"?",{"type":19,"tag":60,"props":494,"children":496},{"style":495},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[497],{"type":24,"value":498}," 1",{"type":19,"tag":60,"props":500,"children":501},{"style":109},[502],{"type":24,"value":503}," :",{"type":19,"tag":60,"props":505,"children":506},{"style":495},[507],{"type":24,"value":508}," 0",{"type":19,"tag":60,"props":510,"children":511},{"style":79},[512],{"type":24,"value":203},{"type":19,"tag":60,"props":514,"children":515},{"class":62,"line":231},[516],{"type":19,"tag":60,"props":517,"children":518},{"style":79},[519],{"type":24,"value":270},{"type":19,"tag":41,"props":521,"children":523},{"id":522},"rounding-to-n-decimal-places",[524],{"type":24,"value":525},"Rounding to n decimal places",{"type":19,"tag":48,"props":527,"children":529},{"className":50,"code":528,"language":52,"meta":53,"style":53},"public decimal ArithmeticRound(decimal d, int decimals) {\n    var power = (decimal)Math.Pow(10, decimals);\n    return (decimal.Floor((Math.Abs(d) * power) + 0.4m) / power) * Math.Sign(d);\n}\n",[530],{"type":19,"tag":56,"props":531,"children":532},{"__ignoreMap":53},[533,583,632,723],{"type":19,"tag":60,"props":534,"children":535},{"class":62,"line":63},[536,540,545,550,554,559,564,568,573,578],{"type":19,"tag":60,"props":537,"children":538},{"style":109},[539],{"type":24,"value":112},{"type":19,"tag":60,"props":541,"children":542},{"style":136},[543],{"type":24,"value":544}," decimal",{"type":19,"tag":60,"props":546,"children":547},{"style":121},[548],{"type":24,"value":549}," ArithmeticRound",{"type":19,"tag":60,"props":551,"children":552},{"style":79},[553],{"type":24,"value":164},{"type":19,"tag":60,"props":555,"children":556},{"style":136},[557],{"type":24,"value":558},"decimal",{"type":19,"tag":60,"props":560,"children":561},{"style":312},[562],{"type":24,"value":563}," d",{"type":19,"tag":60,"props":565,"children":566},{"style":79},[567],{"type":24,"value":185},{"type":19,"tag":60,"props":569,"children":570},{"style":136},[571],{"type":24,"value":572},"int",{"type":19,"tag":60,"props":574,"children":575},{"style":312},[576],{"type":24,"value":577}," decimals",{"type":19,"tag":60,"props":579,"children":580},{"style":79},[581],{"type":24,"value":582},") {\n",{"type":19,"tag":60,"props":584,"children":585},{"class":62,"line":95},[586,590,595,599,604,608,613,618,622,627],{"type":19,"tag":60,"props":587,"children":588},{"style":136},[589],{"type":24,"value":139},{"type":19,"tag":60,"props":591,"children":592},{"style":79},[593],{"type":24,"value":594}," power ",{"type":19,"tag":60,"props":596,"children":597},{"style":109},[598],{"type":24,"value":149},{"type":19,"tag":60,"props":600,"children":601},{"style":79},[602],{"type":24,"value":603}," (",{"type":19,"tag":60,"props":605,"children":606},{"style":136},[607],{"type":24,"value":558},{"type":19,"tag":60,"props":609,"children":610},{"style":79},[611],{"type":24,"value":612},")Math.",{"type":19,"tag":60,"props":614,"children":615},{"style":121},[616],{"type":24,"value":617},"Pow",{"type":19,"tag":60,"props":619,"children":620},{"style":79},[621],{"type":24,"value":164},{"type":19,"tag":60,"props":623,"children":624},{"style":495},[625],{"type":24,"value":626},"10",{"type":19,"tag":60,"props":628,"children":629},{"style":79},[630],{"type":24,"value":631},", decimals);\n",{"type":19,"tag":60,"props":633,"children":634},{"class":62,"line":105},[635,639,643,647,651,656,661,666,671,676,681,686,691,695,700,704,708,713,718],{"type":19,"tag":60,"props":636,"children":637},{"style":67},[638],{"type":24,"value":237},{"type":19,"tag":60,"props":640,"children":641},{"style":79},[642],{"type":24,"value":603},{"type":19,"tag":60,"props":644,"children":645},{"style":136},[646],{"type":24,"value":558},{"type":19,"tag":60,"props":648,"children":649},{"style":79},[650],{"type":24,"value":82},{"type":19,"tag":60,"props":652,"children":653},{"style":121},[654],{"type":24,"value":655},"Floor",{"type":19,"tag":60,"props":657,"children":658},{"style":79},[659],{"type":24,"value":660},"((Math.",{"type":19,"tag":60,"props":662,"children":663},{"style":121},[664],{"type":24,"value":665},"Abs",{"type":19,"tag":60,"props":667,"children":668},{"style":79},[669],{"type":24,"value":670},"(d) ",{"type":19,"tag":60,"props":672,"children":673},{"style":109},[674],{"type":24,"value":675},"*",{"type":19,"tag":60,"props":677,"children":678},{"style":79},[679],{"type":24,"value":680}," power) ",{"type":19,"tag":60,"props":682,"children":683},{"style":109},[684],{"type":24,"value":685},"+",{"type":19,"tag":60,"props":687,"children":688},{"style":495},[689],{"type":24,"value":690}," 0.4m",{"type":19,"tag":60,"props":692,"children":693},{"style":79},[694],{"type":24,"value":406},{"type":19,"tag":60,"props":696,"children":697},{"style":109},[698],{"type":24,"value":699},"/",{"type":19,"tag":60,"props":701,"children":702},{"style":79},[703],{"type":24,"value":680},{"type":19,"tag":60,"props":705,"children":706},{"style":109},[707],{"type":24,"value":675},{"type":19,"tag":60,"props":709,"children":710},{"style":79},[711],{"type":24,"value":712}," Math.",{"type":19,"tag":60,"props":714,"children":715},{"style":121},[716],{"type":24,"value":717},"Sign",{"type":19,"tag":60,"props":719,"children":720},{"style":79},[721],{"type":24,"value":722},"(d);\n",{"type":19,"tag":60,"props":724,"children":725},{"class":62,"line":132},[726],{"type":19,"tag":60,"props":727,"children":728},{"style":79},[729],{"type":24,"value":270},{"type":19,"tag":20,"props":731,"children":732},{},[733],{"type":19,"tag":29,"props":734,"children":735},{},[736],{"type":24,"value":33},{"type":19,"tag":738,"props":739,"children":740},"style",{},[741],{"type":24,"value":742},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":53,"searchDepth":95,"depth":95,"links":744},[745,746,747],{"id":43,"depth":95,"text":46},{"id":273,"depth":95,"text":276},{"id":522,"depth":95,"text":525},"markdown","content:blog:2006:dotnettipsuptimeagerounding.md","content","blog/2006/dotnettipsuptimeagerounding.md","blog/2006/dotnettipsuptimeagerounding","md","/blog/2006/dotnettipsuptimeagerounding/",195,0,[758,762,766],{"title":759,"date":760,"url":761},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":763,"date":764,"url":765},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":767,"date":768,"url":769},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[],1779264594944]