Monday, January 21, 2019

how to use toLocaleString() and tofixed(2) in javascript


numObj.toLocaleString([locales [, options]])

toLocaleString takes 2 arguments. The first is the locale, the second are the options. As for the options, you are looking for:

minimumFractionDigits

The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information).

To be able to set the options without setting the locale, you can pass undefined as first argument:

var num = 2046430;
num.toLocaleString(undefined, {minimumFractionDigits: 2}) // 2,046,430.00

However this also allows the fraction to be longer than 2 digits. So we need to look for one more option called maximumFractionDigits. (Also on that MDN page)

var num = 2046430.123;
num.toLocaleString(undefined, {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
}) // 2,046,430.12

Tuesday, January 1, 2019

Get Height and Width of a Cell in Excel

Private Sub Worksheet_Activate()
    Dim getHeight As Single
    Dim getWidth As Single
   
    For i = 1 To 10
        getHeight = Range("A" + Trim(Str(i))).Height
        getWidth = Range(Chr(65 + i) + "1").Width
     
        Me.UsedRange(i, 1) = "Row  cm : " + Str(Round((getHeight / 72) * 2.54, 2))
        Me.UsedRange(1, i + 1) = Chr(65 + i) + "1 :" + Str(Round((getWidth / 72) * 2.54, 2))
     
    Next
End Sub