在算術表達式中使用字符串是否安全? (Is it safe to use strings in arithmetic expressions?)


問題描述

在算術表達式中使用字符串是否安全? (Is it safe to use strings in arithmetic expressions?)

<div class="snippet" data‑lang="js" data‑hide="false" data‑console="true" data‑babel="false">

const result = +'5';

console.log(result, typeof(result));</code></pre> </div > </div> </p>

在文檔中我只找到了關於 Number() 構造函數。

我的問題是:對字符串使用算術是否安全(加法除外)?我可以依賴上面顯示的行為嗎?

/div> </div> </p>

在文檔中我只找到了關於 Number() 構造函數。

我的問題是:對字符串使用算術是否安全(加法除外)?我可以依賴上面顯示的行為嗎?

/div> </div> </p>

在文檔中我只找到了關於 Number() 構造函數。

我的問題是:對字符串使用算術是否安全(加法除外)?我可以依賴上面顯示的行為嗎?

在字符串上使用算術是否安全(加法除外)?我可以依賴上面顯示的行為嗎?</p> 在字符串上使用算術是否安全(加法除外)?我可以依賴上面顯示的行為嗎?</p>


參考解法

方法 1:

Yes, it is safe. All arithmetic operations except a binary + will convert the operands to numbers. That includes bitwise operators as well as unary plus.

With that said, it is probably a good idea not to rely on this extensively. Imagine that you have this code:

function calculate(a, b) {
  return a * 2 + b * 3;
}

//elsewhere in the code
console.log(calculate("5", "2"));

This works fine because both a and b are multiplied, so are going to be converted to numbers. But in six months time you come back to the project and realise you want to modify the calculation, so you change the function:

function calculate(a, b) {
  return a + b * 3;
}

//elsewhere in the code
console.log(calculate("5", "2"));

...and suddenly the result is wrong.

It is therefore better if you explicitly convert the values to numbers if you want to do arithmetic. Saves the occasional accidental bug and it is more maintainable.

方法 2:

Yes, but you have to be careful...

console.log('5.3' * 3);

console.log('5.3' + 3);

These two very similar functions cast the values different ways:

  • * can only be applied between two numbers, so '5.3' becomes 5.3
  • + can also concatenate strings, and the string comes first, so 3 becomes '3'

If you understand all these you can do this, but I'd recommend against it. It's very easy to miss and JS has a lot of weird unexpected casts.

(by anotherOneVLAZKeith)

參考文件

  1. Is it safe to use strings in arithmetic expressions? (CC BY‑SA 2.5/3.0/4.0)

#string #javascript #numbers #math






相關問題

VB.net 如何讓流閱讀器忽略某些行? (VB.net how to make stream reader ignore some line?)

Perl Text::CSV_XS 從字符串中讀取 (Perl Text::CSV_XS read from string)

在 D3 中用逗號格式化數字 (Formatting numbers with commas in D3)

我應該使用什麼-String 或 StringBuilder 將 SQL 查詢存儲在使用許多不同 SQL 查詢的代碼中 (what should i use-String or StringBuilder for storing SQL queries in a code which uses many different SQL queries)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

使用正則表達式處理字符串 (String Manipulation with regular expression)

如何區分數字字符串和字符串? (How do i distinguish between number string and character string?)

如何將單詞的結尾與 Ruby 中的哈希進行比較? (How can I compare the ending of a word with a hash in Ruby?)

在 Python 列表中查找位於特定字符串之間的字符串 (Find Strings Located Between Specific Strings in List Python)

大寫替代字母 (Alternate letters in upper case)

查找 ia strn 列在同一數據框中的列表列中,並創建具有值的第三列 (Find ia a strn column is in a list column in the same data frame and create a 3rd column with a value)

在算術表達式中使用字符串是否安全? (Is it safe to use strings in arithmetic expressions?)







留言討論