19 Oct 2012

Rounding in SQL Server


-- Rounding Off to 0.5
SELECT 8.4 Value,
       CASE
         WHEN 8.4 % 1 >= 0.5 THEN 8.4 - 8.4%0.5
         ELSE Round(8.4, 0)
       END FinalValue

SELECT 8.9 Value,
       CASE
         WHEN 8.9 % 1 >= 0.5 THEN 8.9 - 8.9%0.5
         ELSE Round(8.9, 0)
       END FinalValue
--Rounding Off to 0.0 and getting rounded value
SELECT 8.4                 Value,
       Round(8.4, 0) - 8.4 RoundedOff,
       Round(8.4, 0)       FinalValue

SELECT 8.9                 Value,
       Round(8.9, 0) - 8.9 RoundedOff,
       Round(8.9, 0)       FinalValue


12 Oct 2012

Centering a Div Both Horizontally And Vertically


While building web page layouts, you’ve probably been faced with a situation where you need to center a div both horizontally and vertically with pure CSS. There are more than a few ways to achieve this, and in this MicroTut I am going to show you my favorite involving CSS and jQuery.
But first, the basics:
Horizontal centering with CSS
It is as easy as applying a margin to a div:
.className{
    margin:0 auto;
    width:200px;
    height:200px;
}
To center a div only horizontally, you need to specify a width, and an auto value for the left and right margins (you do remember the shorthand declarations in CSS don’t you?). This method works on block level elements (divs, paragraphs, h1, etc). To apply it to inline elements (like hyperlinks and images), you need to apply one additional rule – display:block.
Horizontal and vertical centering with CSS
Center a div both horizontally and vertically with CSS is a bit more tricky. You need to know the dimensions of the div beforehand.
.className{
    width:300px;
    height:200px;
    position:absolute;
    left:50%;
    top:50%;
    margin:-100px 0 0 -150px;
}
By positioning the element absolutely, we can detach it from its surroundings and specify its position in relation to the browser window. Offsetting the div by 50% from the left and the top part of the window, you have its upper-left corner precisely at the center of the page. The only thing we are left to do is to move the div to the left and to the top with half its width and height with a negative margin, to have it perfectly centered.
Horizontal and vertical centering with jQuery
As mentioned earlier – the CSS method only works with divs with fixed dimensions. This is where jQuery comes into play:
$(window).resize(function(){
    $('.className').css({
        position:'absolute',
        left: ($(window).width() - $('.className').outerWidth())/2,
        top: ($(window).height() - $('.className').outerHeight())/2
    });
});
// To initially run the function:
$(window).resize();
The functionality is inserted into a $(window).resize() statement, which is executed every time the window is resized by the user. We use outerWidth() and outerHeight(), because unlike from the regular width() and height(), they add the padding and the border width to the returned size. Lastly, we simulate a resize event to kick center the div on page load.
The benefit of using this method, is that you do not need to know how big the div is. The main disadvantage is that it will only work with JavaScript turned on. This however makes it perfect for rich user interfaces