Skip to content

Sign of a number · ⁨数的符号⁩

English

Sign of a number

Return a number that describes the sign of n: -1 when n is negative, 1 when it is positive, and 0 when it is exactly zero.

Use if to test the cases. The order matters — handle negative and positive first, then return 0 for what is left.

中文

数的符号

返回一个表示 n 符号的数:当 n 为负时返回 -1,为正时返回 1,恰好为零时返回 0。

用 if 来判断这几种情况。顺序很重要 —— 先处理负数和正数,剩下的情况再返回 0。

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩