# num-is-zero

> 判斷數值是否為零 (Zero)，並將負零 (-0) 正規化 (Normalize) 為 0 的工具函式

Latest version **2.0.1** (published 2026-10-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install num-is-zero
pnpm add num-is-zero
yarn add num-is-zero
bun add num-is-zero
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2026-10-05 |
| First published | 2020-06-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 30 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | bluelovers |
| Maintainers | bluelovers |
| Keywords | num-is-zero, zero, is-zero, negative-zero, normalize, type-guard, number, check, fix-zero, create-by-yarn-tool, create-by-tsdx |

## Links

- npm: https://www.npmjs.com/package/num-is-zero
- Repository: https://github.com/bluelovers/ws-random
- Homepage: https://github.com/bluelovers/ws-random/tree/master/packages/num-is-zero#readme
- Issues: https://github.com/bluelovers/ws-random/issues
- npm.io page: https://npm.io/package/num-is-zero

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2026-10-05
- 1.0.13 — 2023-11-19
- 1.0.11 — 2022-10-29
- 1.0.10 — 2022-01-03
- 1.0.8 — 2021-12-12
- 1.0.2 — 2020-06-26

## README

# num-is-zero

判斷數值是否為零 (Zero)，並可將負零 (`-0`) 正規化 (Normalize) 為 `0` 的小型工具函式。

## 特色 (Features)

- `isZero` 以型別守衛 (Type Guard) 回傳，可在 TypeScript 中直接窄化 (Narrow) 型別為 `0`。
- 同時涵蓋 `0` 與 `-0` 的判斷，避免負零造成漏判。
- `fixZero` 可將 `-0` 統一轉為 `0`，讓後續序列化 (Serialization) 或顯示結果一致。
- 零相依 (Zero Dependencies)，並同時支援 CommonJS 與 ESM。

## 安裝 (Installation)

```bash
npm install num-is-zero
```

```bash
yarn add num-is-zero
yarn-tool add num-is-zero
yt add num-is-zero
```

## 使用方式 (Usage)

```js
import isZero, { fixZero } from 'num-is-zero'

isZero(0)      // true
isZero(-0)     // true
isZero(1)      // false
isZero('0')    // false，僅判斷嚴格相等的數值

fixZero(-0)    // 0
fixZero(0)     // 0
fixZero(1)     // 1
```

## API 文件

### isZero(val)

判斷傳入值是否為零，為本套件的預設匯出 (Default Export)。

| 參數 | 型別 | 說明 |
| --- | --- | --- |
| `val` | `unknown` | 要檢查的任意值 |

**回傳值 (Returns)**：`val is 0` — 型別守衛，透過後可將 `val` 視為 `0`。

```ts
import isZero from 'num-is-zero'

function demo(val: number)
{
	if (isZero(val))
	{
		// val 的型別窄化為 0
		console.log(val.toFixed(2))
	}
}
```

### fixZero(val)

若傳入值為負零 (`-0`) 則回傳 `0`，否則原樣回傳。

| 參數 | 型別 | 說明 |
| --- | --- | --- |
| `val` | `T` | 要正規化的值 |

**回傳值 (Returns)**：`T` — `-0` 會被替換為 `0`，其餘值不變。

```js
import { fixZero } from 'num-is-zero'

Object.is(fixZero(-0), 0) // true
```

## 開發 (Development)

在 monorepo 根目錄或本套件目錄下執行：

```bash
pnpm run test:jest
pnpm run build
```

## 變更日誌 (Changelog)

請見 [CHANGELOG.md](./CHANGELOG.md)。

---
_Source: https://npm.io/package/num-is-zero · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
