Truncate decimal places with C#

Hi there, I just want to share with you on how to properly truncate decimal places.

Code Snippet
  1. public static class DecimalExtensions
  2. {
  3.     public static decimal TruncateEx(this decimal value, int decimalPlaces)
  4.     {
  5.         if (decimalPlaces < 0)
  6.             throw new ArgumentException(“decimalPlaces must be greater than or equal to 0.”);
  7.  
  8.         if (value == 0) return decimal.Zero;
  9.  
  10.         var modifier = Convert.ToDecimal(0.5 / Math.Pow(10, decimalPlaces));
  11.         return Math.Round(value > 0 ? value – modifier : value + modifier, decimalPlaces, MidpointRounding.AwayFromZero);
  12.     }
  13. }

Example usage :

(9.577M).TruncateEx(2); //9.57

(119.577M).TruncateEx(2); //119.57

Kudos to https://stackoverflow.com/users/94990/nightcoder for the solution. I had added some code to fix bugs regarding MidpointRounding and wrong handling with zero argument.

This extension method is a very neat solution. It will give you immediate access to the truncate method from any decimal in your code.

My 3rd SNAP Lettuce set for 2019

The spot currently is getting adequate sunlight. I am still using seeds from RAMGO. Seeds can be bought at any Ace Hardware store.

Also I am trying to grow my seedlings for my 4th set this year. I have covered the seedlings with plain plastic cover. This is the first time I used plain plastic cover, it would be nice if it is sufficient.

When I grow seedlings, I make sure I try to give them shade. But once they are transplanted you can give them 4 hours of direct sunlight. Even if they extremely wilt they will still recover a few hours after.

You should see my lettuce at night. They look very strong and wants to grow bigger.

WPF – Touchdown event firing at overlapping controls.

This happens when a button or link behind another button or link is getting its touchdown event fired as well.

The fix here:

1. Make sure tap event of both controls are subscribed on the Touchdown event.
2. Put a e.Handled = true; statement inside the eventhandler of the overlapping control.

Example:

Code Snippet
  1. private void OpenDoctor_Touch(object sender, TouchEventArgs e)
  2. {
  3.     e.Handled = true;
  4.     string commandParameter = ((Button)sender).CommandParameter.ToString();
  5.     var viewModel = (DoctorsBySpecializationViewModel)DataContext;
  6.     if (viewModel.OpenDoctor.CanExecute(commandParameter))
  7.         viewModel.OpenDoctor.Execute(commandParameter);
  8. }

Happy Coding!

WPF and MVVM – Touch Device needs to tap twice on button to fire command.

Touch devices may need to tap twice on a button to fire command.

This always happens on my end when the button is located alongside a textbox or other elements on a user or windows control.

The fix.

1. Add a touch down event for the event in the xaml.

2. Fire command manually

            MapEntity commandParameter = (MapEntity)(((Button)sender).CommandParameter);

            var viewModel = (FindADISViewModel)DataContext;
            if (viewModel.OpenDIS.CanExecute(commandParameter))
                viewModel.OpenDIS.Execute(commandParameter);

It works!

SSRS Matrix – Sum fields in a row group but avoid duplicates

If you want to avoid the duplicates for a row group field for the grand total of a row group. Use builtin MAX or MIN aggregate functions for the field that will be used in the sum function.

Wrong implementation
= SUM(Fields!Amount.Value)

Correct Implementation
= SUM(MAX(Fields!Amount.Value, “NameOfRowGroup”))

 

Lenovo Thinkpad E450 – Windows 10 Noisy CPU Fan and eventual BSOD

The BSOD happens usually when I extend my display. I work better having at least 2 monitors. I am happy I was able to extend up to 3 monitors using USB to VGA adapter.

First of all here is my laptop specs
CPU – Intel Core I7 5500U Broadwell
Memory – 16 GB Ram
Storage – 480 GB SATA 3.0 SKS MLC SSD
Graphics – onboard Intel HD Graphics 5500

Solution:
Please follow this two fixes.
1. Disable AMD Driver at the device manager

2. Avoid toggling enable/disable of extended monitors using Intel Graphics software. My recommendation is when you need to toggle enable/disable monitors you need to turn off your laptop. Plug or unplug HDMI to follow.

I hope this helps for you!

Promoted to MCSA! Passed 70-762 Microsoft Certification Exam

When studying for this particular exam, a lot of built in SQL Objects like views and functions are mentioned throughout the reference book.

The supposed to be easiest preparation turned out to be most stressful since I had to memorize a lot of those objects.

For passing this exam. I bought an e-book by Microsoft. This is the official exam reference book for this exam by Louis Davidson and Stacia Varga. I have read the book 3 times and I practice on-hand when in the mood.

Also I needed to read from these following urls:

  • https://www.sqlshack.com/locking-sql-server/

My preparation lasted for 7 months. On average I dedicated about 2.5 hours a day everyday reading and practicing! It was a stressful 7 months, I had a lot of momentary pain in the head as if my skull is trying to compress. Saturdays/Sundays and holidays were dedicated to preparation. I still had the time to enjoy beer and get wasted.

This was also one out of the two exams you need to pass to attain MCSA. I finally am an MCSA. Thanks for Intelligent Touch Corporation, my employer who paid for this exam. Thanks for the continued patronage.

Posted below is the new badges I acquired. Also below I posted my score.