how to truncate a float to a either an int or no fractional bit

时间:2015-05-12 22:27:42

标签: swift

I have a float that we'd like to truncate the fractional part off of but not sure the easiest way to do this. Just picking up Swift and most of the thoughts seemed way too involved. Currently I have:

var {Cc, Ci} = require('chrome');
var sss = Cc['@mozilla.org/content/style-sheet-service;1'].getService(Ci.nsIStyleSheetService);
var ios = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
var windowUtils = require('window-utils');
var wm = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);

exports.main = function() {
    //nothing to export
};

var aWin = wm.getMostRecentWindow('navigator:browser'); //aWin stands for anyWindow. this is used by stuff to use functions like encodeURI

//hide the extended label when a page is verified
var identityBoxCss = '.verifiedDomain .plain {display:none}';
var identityBoxCssData = 'data:text/css;charset=utf-8,' + aWin.encodeURI(identityBoxCss);
var identityBoxCssUri = ios.newURI(identityBoxCssData, null, null);
//end hide the extended label when a page is verified
//url bar colorize
var gUrlBarColorize = {
    func: function(win) {
        var identityIconLabel = win.document.getElementById('identity-icon-label');
        var identityBox = win.document.getElementById('identity-box');
        var gURLBar = win.gURLBar;
        if (gURLBar.value.search(/https/i) == 0) {
            if ((identityBox.classList.contains('verifiedDomain') || identityBox.classList.contains('verifiedIdentity'))&& identityIconLabel.value.length > 0) {
                gURLBar.style.backgroundColor = '#D0F7B9';
            } else {
                gURLBar.style.backgroundColor = '#F8D6DE';
            }
        } else {
            gURLBar.style.backgroundColor = '';
        }
    }
};
//end - url bar colorize
//hide icons in bookmarks toolbar
var bookmarksToolbarCss = '.bookmark-item .toolbarbutton-icon {display:none}';
var bookmarksToolbarCssData = 'data:text/css;charset=utf-8,' + aWin.encodeURI(bookmarksToolbarCss);
var bookmarksToolbarCssUri = ios.newURI(bookmarksToolbarCssData, null, null);
//end - hide icons in bookmarks toolbar

var wt = new windowUtils.WindowTracker({
    onTrack: function (window) {
        //hide the extended label when a page is verified
        var IdentityBox = window.document.getElementById('identity-box');
        if (IdentityBox) {
            if (sss.sheetRegistered(identityBoxCssUri, sss.USER_SHEET)) {
                sss.unregisterSheet(identityBoxCssUri, sss.USER_SHEET);
            }
            sss.loadAndRegisterSheet(identityBoxCssUri, sss.USER_SHEET);
        }
        //end hide the extended label when a page is verified
        //url bar colorize
        if (window.gBrowser) {
            window.FF7TweaksForScot = {}
            window.FF7TweaksForScot.gUrlBarColorize = function() { gUrlBarColorize.func(window) };
            window.FF7TweaksForScot.gUrlBarColorize();
            window.gBrowser.addEventListener('load', window.FF7TweaksForScot.gUrlBarColorize, true);
            window.gBrowser.addEventListener('pageshow', window.FF7TweaksForScot.gUrlBarColorize, true);
            window.gBrowser.tabContainer.addEventListener('TabSelect', window.FF7TweaksForScot.gUrlBarColorize, true);
        }
        //end - url bar colorize
        //hide icons in bookmarks toolbar
        var PlacesToolbarItems = window.document.getElementById('PlacesToolbarItems');
        if (PlacesToolbarItems) {
            if (sss.sheetRegistered(bookmarksToolbarCssUri, sss.USER_SHEET)) {
                sss.unregisterSheet(bookmarksToolbarCssUri, sss.USER_SHEET);
            }
            sss.loadAndRegisterSheet(bookmarksToolbarCssUri, sss.USER_SHEET);
        }
        //end - hide icons in bookmarks toolbar
    },
    onUntrack: function (window) {
        //hide the extended label when a page is verified
        var IdentityBox = window.document.getElementById('identity-box');
        if (IdentityBox) {
            if (sss.sheetRegistered(identityBoxCssUri, sss.USER_SHEET)) {
                sss.unregisterSheet(identityBoxCssUri, sss.USER_SHEET);
            }
        }
        //end hide the extended label when a page is verified
        //url bar colorize
        if (window.gBrowser) {
            window.gURLBar.style.backgroundColor = '';
            window.gBrowser.removeEventListener('load', window.FF7TweaksForScot.gUrlBarColorize, true);
            window.gBrowser.removeEventListener('pageshow', window.FF7TweaksForScot.gUrlBarColorize, true);
            window.gBrowser.tabContainer.removeEventListener('TabSelect', window.FF7TweaksForScot.gUrlBarColorize, true);
            delete window.FF7TweaksForScot;
        }
        //end - url bar colorize
        //hide icons in bookmarks toolbar
        var PlacesToolbarItems = window.document.getElementById('PlacesToolbarItems');
        if (PlacesToolbarItems) {
            if (sss.sheetRegistered(bookmarksToolbarCssUri, sss.USER_SHEET)) {
                sss.unregisterSheet(bookmarksToolbarCssUri, sss.USER_SHEET);
            }
        }
        //end - hide icons in bookmarks toolbar
    }
});

but this is giving me a fraction which I want removed.

1 个答案:

答案 0 :(得分:2)

We need to combine two things:

  1. $scope.$watch($scope.getWidth, $scope.adjustSidebar); $scope.adjustSidebar = function (newValue, oldValue) { if (newValue >= mobileView) { if (angular.isDefined($cookieStore.get('sidebarToggle'))) { $scope.sidebarToggle = $cookieStore.get('sidebarToggle'); } else { $scope.sidebarToggle = true; } } else { $scope.sidebarToggle = false; } }; 's String initializer
  2. format: operation which will make sure our decimal number doesn't round up.

If we want to keep these operations in-line, we'd want something that looks like this:

floor

But that's a lot of parenthesis. Looks a little better if we unnest this a bit.

details.details.append((titles[2], String(format: "%.0f kW", floor(averageAnnualSolarProduction))))

For clarity here, let solarProduction = String(format: "%.0f kW", floor(averageAnnualSolarProduction)) details.details.append((titles[2], solarProduction)) takes a number like floor and returns the largest integer number smaller than what was passed in, so it would return 3.89 here.

And to be sure that we're not printing the 3.0, we use .0's String initializer, which takes a format string and arguments. This works just like format strings have worked since at least C. format: specifies our argument is a floating point number, and the %f specifies that we will display zero numbers after the decimal point.

If we're find with rounding up, we can drop the call to .0 and simply use the floor initializer, but for a value of format:, this would give us a string that looks like 3.89.