mirror of
https://github.com/jbranchaud/til
synced 2026-01-16 21:48:02 +00:00
Compare commits
3 Commits
5cb7d74933
...
544d580ede
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
544d580ede | ||
|
|
6fad82d653 | ||
|
|
cee26f173d |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1325 TILs and counting..._
|
||||
_1327 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -429,6 +429,7 @@ _1325 TILs and counting..._
|
||||
- [Find The Version Of An Installed Dependency](javascript/find-the-version-of-an-installed-dependency.md)
|
||||
- [Find Where Yarn Is Installing Binaries](javascript/find-where-yarn-is-installing-binaries.md)
|
||||
- [for...in Iterates Over Object Properties](javascript/for-in-iterates-over-object-properties.md)
|
||||
- [Format A Decimal To A Fixed Number Of Digits](javascript/format-a-decimal-to-a-fixed-number-of-digits.md)
|
||||
- [Formatting Values With Units For Display](javascript/formatting-values-with-units-for-display.md)
|
||||
- [Freeze An Object, Sorta](javascript/freeze-an-object-sorta.md)
|
||||
- [Generate A V4 UUID In The Browser](javascript/generate-a-v4-uuid-in-the-browser.md)
|
||||
@@ -1346,6 +1347,7 @@ _1325 TILs and counting..._
|
||||
- [Watch This Run Repeatedly](unix/watch-this-run-repeatedly.md)
|
||||
- [Where Are The Binaries?](unix/where-are-the-binaries.md)
|
||||
- [xargs Default Command Is echo](unix/xargs-default-command-is-echo.md)
|
||||
- [xargs Ignores Alias Substitution By Default](unix/xargs-ignores-alias-substitution-by-default.md)
|
||||
|
||||
### Vercel
|
||||
|
||||
|
||||
52
javascript/format-a-decimal-to-a-fixed-number-of-digits.md
Normal file
52
javascript/format-a-decimal-to-a-fixed-number-of-digits.md
Normal file
@@ -0,0 +1,52 @@
|
||||
# Format A Decimal To A Fixed Number Of Digits
|
||||
|
||||
The `Intl.NumberFormat` object is a reliable way to format numbers in an
|
||||
i18n-friendly way. It available with Node and all modern browsers.
|
||||
|
||||
If I want to format number that I expect to contain decimals, I can do so by
|
||||
setting up a formatter using the `decimal` style.
|
||||
|
||||
```javascript
|
||||
const locale = "en-US";
|
||||
const options = {
|
||||
style: "decimal",
|
||||
};
|
||||
|
||||
const formatter = new Intl.NumberFormat(locale, options);
|
||||
|
||||
console.log(formatter.format(1234))
|
||||
//=> 1,234
|
||||
console.log(formatter.format(1234.5678))
|
||||
//=> 1,234.568
|
||||
```
|
||||
|
||||
Because of my locale (`en-US`), it adds a comma to deliniate the thousandth
|
||||
place. By default, it formats to three decimal places excluding decimals
|
||||
altogether if it is a whole number.
|
||||
|
||||
If I want to specify a fixed number of decimal places including for a whole
|
||||
number, I can use the `minimumFractionDigits` and `maximumFractionDigits`
|
||||
options.
|
||||
|
||||
```javascript
|
||||
const locale = "en-US";
|
||||
const options = {
|
||||
style: "decimal",
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
};
|
||||
|
||||
const formatter = new Intl.NumberFormat(locale, options);
|
||||
|
||||
console.log(formatter.format(1234))
|
||||
//=> 1,234.00
|
||||
console.log(formatter.format(1234.5678))
|
||||
//=> 1,234.57
|
||||
```
|
||||
|
||||
Here, it includes the `.00` on the whole number and it truncates the number
|
||||
with more than 2 decimal places rounding as necessary.
|
||||
|
||||
See the [`Intl.NumberFormat`
|
||||
docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat)
|
||||
for more details.
|
||||
@@ -21,7 +21,7 @@ Hit `<prefix>:` to issue the following command to `tmux`:
|
||||
Or issue the command from one of your pane's terminal prompts:
|
||||
|
||||
```
|
||||
$ tmxu set pane-border-status top
|
||||
$ tmux set pane-border-status top
|
||||
```
|
||||
|
||||
Either way, it will replace the top border line with text telling you the pane
|
||||
|
||||
33
unix/xargs-ignores-alias-substitution-by-default.md
Normal file
33
unix/xargs-ignores-alias-substitution-by-default.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# xargs Ignores Alias Substitution By Default
|
||||
|
||||
I have a number of aliases set up in my shell's RC file. For instance, I use
|
||||
`nvim` as my main editor, but because of muscle memory, I've aliased `vim` to
|
||||
`nvim`.
|
||||
|
||||
```bash
|
||||
❯ alias vim
|
||||
vim=nvim
|
||||
```
|
||||
|
||||
So, I was surprised when I ran the following `xargs` command.
|
||||
|
||||
```bash
|
||||
❯ rg 'some pattern' -l | xargs vim
|
||||
```
|
||||
|
||||
It opened the matching files in `vim` rather than `nvim`.
|
||||
|
||||
The reason for this is that `xargs` is a separate function that does not have
|
||||
an internal concept of aliases that need to be substituted.
|
||||
|
||||
There is, however, a trick built in to `alias` that we can use. By leaving a
|
||||
trailing space in an alias, we tell the shell to check for an alias
|
||||
substitution to expand in the following word.
|
||||
|
||||
So, I can alias `xargs` to `'xargs '` and it will respect my `vim` alias.
|
||||
|
||||
```
|
||||
❯ alias xargs='xargs '
|
||||
```
|
||||
|
||||
[source](https://unix.stackexchange.com/a/244516/5916)
|
||||
Reference in New Issue
Block a user