Hướng dẫn trim comma in javascript - cắt dấu phẩy trong javascript

Câu trả lời liên quan, nhưng nếu bạn muốn chạy dọn dẹp các giá trị nhập người dùng vào biểu mẫu, thì đây là những gì bạn có thể làm:

Nội dung chính

  • Xóa tất cả các dấu phẩy khỏi chuỗi #
  • Đọc thêm #
  • Làm thế nào để bạn loại bỏ dấu phẩy khỏi một chuỗi?
  • Làm cách nào để loại bỏ dấu phẩy cuối cùng khỏi một chuỗi trong java?
  • Làm thế nào để bạn loại bỏ dấu phẩy khỏi một mảng?
  • Làm cách nào để loại bỏ dấu phẩy trong React?

const numFormatter = new Intl.NumberFormat('en-US', {
  style: "decimal",
  maximumFractionDigits: 2
})

// Good Inputs
parseFloat(numFormatter.format('1234').replace(/,/g,"")) // 1234
parseFloat(numFormatter.format('123').replace(/,/g,"")) // 123

// 3rd decimal place rounds to nearest
parseFloat(numFormatter.format('1234.233').replace(/,/g,"")); // 1234.23
parseFloat(numFormatter.format('1234.239').replace(/,/g,"")); // 1234.24

// Bad Inputs
parseFloat(numFormatter.format('1234.233a').replace(/,/g,"")); // NaN
parseFloat(numFormatter.format('$1234.23').replace(/,/g,"")); // NaN

// Edge Cases
parseFloat(numFormatter.format(true).replace(/,/g,"")) // 1
parseFloat(numFormatter.format(false).replace(/,/g,"")) // 0
parseFloat(numFormatter.format(NaN).replace(/,/g,"")) // NaN

Sử dụng ngày quốc tế địa phương thông qua format. Điều này làm sạch bất kỳ đầu vào xấu nào, nếu có, nó sẽ trả về một chuỗi

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
0 mà bạn có thể kiểm tra. Hiện tại không có cách nào để loại bỏ dấu phẩy như một phần của địa phương (kể từ ngày 10/12/19), vì vậy bạn có thể sử dụng lệnh regex để loại bỏ dấu phẩy bằng
updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
1.

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
2 Chuyển đổi định nghĩa loại này từ chuỗi sang số

Nếu bạn sử dụng React, đây là những gì chức năng tính toán của bạn có thể trông như thế nào:

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}

Xóa tất cả các dấu phẩy khỏi chuỗi #

Đọc thêm #

  1. Làm thế nào để bạn loại bỏ dấu phẩy khỏi một chuỗi?
  2. Làm cách nào để loại bỏ dấu phẩy cuối cùng khỏi một chuỗi trong java?

Copied!

const str = '1,23,45.67'; const withoutCommas = str.replaceAll(',', ''); console.log(withoutCommas); // 👉️ '12345.67'

Làm thế nào để bạn loại bỏ dấu phẩy khỏi một mảng?

Làm cách nào để loại bỏ dấu phẩy trong React?

Sử dụng ngày quốc tế địa phương thông qua format. Điều này làm sạch bất kỳ đầu vào xấu nào, nếu có, nó sẽ trả về một chuỗi

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
0 mà bạn có thể kiểm tra. Hiện tại không có cách nào để loại bỏ dấu phẩy như một phần của địa phương (kể từ ngày 10/12/19), vì vậy bạn có thể sử dụng lệnh regex để loại bỏ dấu phẩy bằng
updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
1.

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
2 Chuyển đổi định nghĩa loại này từ chuỗi sang số

Nếu bạn sử dụng React, đây là những gì chức năng tính toán của bạn có thể trông như thế nào:

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
2.

Để xóa tất cả các dấu phẩy khỏi một chuỗi:

Gọi phương thức
updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
3, chuyển nó một dấu phẩy là tham số đầu tiên và một chuỗi trống là thứ hai.2, chuyển nó một biểu thức thông thường để khớp với tất cả các dấu phẩy là tham số đầu tiên và một chuỗi trống làm tham số thứ hai. Phương thức thay thế sẽ trả về một chuỗi mới với tất cả các dấu phẩy bị xóa.

Copied!

const str = '1,23,45.67'; const withoutCommas = str.replace(/,/g, ''); console.log(withoutCommas); // 👉️ '12345.67'

Phương thức

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
4 trả về một chuỗi mới với tất cả các trận đấu được thay thế bằng cách thay thế được cung cấp.

Tham số đầu tiên chúng tôi chuyển đến phương thức chuỗi.RepaceALL là chuỗi chúng tôi muốn khớp và tham số thứ hai là sự thay thế cho mỗi trận đấu.

Phương thức
updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
4 không thay đổi nội dung của chuỗi gốc, nó trả về một chuỗi mới với tất cả các kết quả được thay thế. Chuỗi là bất biến trong JavaScript.

Chúng tôi có thể truyền một biểu thức chính quy như tham số đầu tiên cho phương thức

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
4, nhưng chúng tôi không cần phải làm thế.

Đối với mục đích của chúng tôi, một chuỗi cơ bản chứa dấu phẩy sẽ đủ.

Ngoài ra, bạn có thể sử dụng phương pháp

Để xóa tất cả các dấu phẩy khỏi một chuỗi, hãy gọi phương thức

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
2, chuyển nó một biểu thức thông thường để khớp với tất cả các dấu phẩy là tham số đầu tiên và một chuỗi trống làm tham số thứ hai. Phương thức thay thế sẽ trả về một chuỗi mới với tất cả các dấu phẩy bị xóa.

Đọc thêm #

  • Làm thế nào để bạn loại bỏ dấu phẩy khỏi một chuỗi?
  • Làm cách nào để loại bỏ dấu phẩy cuối cùng khỏi một chuỗi trong java?
  • Làm thế nào để bạn loại bỏ dấu phẩy khỏi một mảng?
  • Làm cách nào để loại bỏ dấu phẩy trong React?
  • Sử dụng ngày quốc tế địa phương thông qua format. Điều này làm sạch bất kỳ đầu vào xấu nào, nếu có, nó sẽ trả về một chuỗi
    updateCalculationInput = (e) => {
        let value;
        value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
        if(value === 'NaN') return; // locale returns string of NaN if fail
        value = value.replace(/,/g, ""); // remove commas
        value = parseFloat(value); // now parse to float should always be clean input
    
        // Do the actual math and setState calls here
    }
    
    0 mà bạn có thể kiểm tra. Hiện tại không có cách nào để loại bỏ dấu phẩy như một phần của địa phương (kể từ ngày 10/12/19), vì vậy bạn có thể sử dụng lệnh regex để loại bỏ dấu phẩy bằng
    updateCalculationInput = (e) => {
        let value;
        value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
        if(value === 'NaN') return; // locale returns string of NaN if fail
        value = value.replace(/,/g, ""); // remove commas
        value = parseFloat(value); // now parse to float should always be clean input
    
        // Do the actual math and setState calls here
    }
    
    1.
  • updateCalculationInput = (e) => {
        let value;
        value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
        if(value === 'NaN') return; // locale returns string of NaN if fail
        value = value.replace(/,/g, ""); // remove commas
        value = parseFloat(value); // now parse to float should always be clean input
    
        // Do the actual math and setState calls here
    }
    
    2 Chuyển đổi định nghĩa loại này từ chuỗi sang số
  • Nếu bạn sử dụng React, đây là những gì chức năng tính toán của bạn có thể trông như thế nào:

Làm thế nào để bạn loại bỏ dấu phẩy khỏi một chuỗi?

Làm cách nào để loại bỏ dấu phẩy cuối cùng khỏi một chuỗi trong java?Call the replaceAll() method, passing it a comma as the first parameter and an empty string as the second. The replaceAll method returns a new string with all matches replaced by the provided replacement.

Làm cách nào để loại bỏ dấu phẩy cuối cùng khỏi một chuỗi trong java?

Làm thế nào để bạn loại bỏ dấu phẩy khỏi một mảng? We remove the last comma of a string by using the built-in substring() method with first argument 0 and second argument string. length()-1 in Java. Slicing starts from index 0 and ends before last index that is string. length()-1 .

Làm thế nào để bạn loại bỏ dấu phẩy khỏi một mảng?

Làm cách nào để loại bỏ dấu phẩy trong React?use the join() method in JavaScript to remove commas from an array. The comma delimiter in an array works as the separator. For example, let arr = ['alpha', 'bravo', 'charlie']. Using the join() method, you can remove or replace all the commas with a space.

Làm cách nào để loại bỏ dấu phẩy trong React?

Phản ứng làm thế nào để loại bỏ dấu phẩy khỏi một chuỗi...

var StringWithCommas = 'a, b, c, d' ;.

var StringWithOutCommas = StringWithCommas.thay thế (/,/g, '') ;.

Bảng điều khiển.log (StringWithOutCommas) ;.