Hướng dẫn how do i use tofixed in react javascript? - làm thế nào để tôi sử dụng được đóng đinh trong javascript phản ứng?

Tôi đang in dữ liệu lấy từ API vào bảng và tôi đang gặp phải một số khó khăn để sửa các giá trị số hàng thành số thập phân. Nếu dữ liệu hàng trong API bao gồm các giá trị số, tức là 10.0, 333, 8 or 100, v.v. để hiển thị nó cuối cùng với các giá trị thập phân -> 10.00, 333.00, 100.00.10.0, 333, 8 or 100, etc. to render it in the end with decimal values -> 10.00, 333.00, 100.00.

Chức năng mà tôi quen thuộc với .toFixed[2], không hoạt động theo cùng một cách trong React, như tôi đã sử dụng để mã hóa nó trong JavaScript. Tôi nghĩ rằng tôi đang đánh lừa tiêu chuẩn ES6, nhưng tôi không chắc chắn. Đây là cách nó trông như thế nào, nếu tôi tránh sử dụng .toFixed[2]..toFixed[2], doesn't function in the same way in React, as I used to code it in javaScript. I think I am misleading the ES6 standard, but I am not sure. Here is how it's look like, if I avoid using .toFixed[2].

Đây là mẫu mã của tôi với rows.toFixed[2], nhưng nó không hoạt động tốt:rows.toFixed[2], but it's doesn't function well:

class App extends React.Component
{
    constructor[]
    {
        super[];
        this.state = {
            rows: [],
            columns: []
        }
    }

    componentDidMount[]
    {

        fetch[ "//ickata.net/sag/api/staff/bonuses/" ]
            .then[ function [ response ]
            {
                return response.json[];
            } ]
            .then[ data =>
            {
                this.setState[ { rows: data.rows, columns: data.columns } ];
            } ];

    }

    render[]
    {

        return [
            

Final Table with React JS

{ this.state.columns.map[[ column, index ] => { return [ {column} ] } ] } { this.state.rows.toFixed[2].map[ row => [ {row.toFixed[2].map[ cell => [ ] ]} ] ] }
{cell}
] } } ReactDOM.render[
, document.querySelector[ 'body' ] ];

Bạn có thể đóng góp trực tiếp vào repo của tôi: tìm nạp dữ liệu API vào bảng

Đây là cách trông giống như ví dụ của tôi, khi .toFixed đã được sử dụng:

Thật không may, tôi đã không thể tìm thấy tài liệu liên quan tại Reactjs.org

Nó cũng không hoạt động với rows[].length.toFixed[2].rows[].length.toFixed[2] either.

Bất kỳ đề xuất sẽ được đánh giá cao!

Phương pháp

toFixed[]
toFixed[digits]
0 định dạng một số sử dụng ký hiệu điểm cố định.
toFixed[]
toFixed[digits]
0
method formats a number using fixed-point notation.

Thử nó

Cú pháp

toFixed[]
toFixed[digits]

Thông số

toFixed[]
toFixed[digits]
1 Tùy chọnOptional

Số chữ số xuất hiện sau điểm thập phân; Đây có thể là một giá trị giữa

toFixed[]
toFixed[digits]
2 và
toFixed[]
toFixed[digits]
3, bao gồm và triển khai có thể tùy chọn hỗ trợ một phạm vi giá trị lớn hơn. Nếu lập luận này bị bỏ qua, nó được coi là
toFixed[]
toFixed[digits]
2.

Giá trị trả về

Một chuỗi đại diện cho số đã cho bằng cách sử dụng ký hiệu điểm cố định.

Ngoại lệ

toFixed[]
toFixed[digits]
5

Nếu

toFixed[]
toFixed[digits]
1 quá nhỏ hoặc quá lớn. Các giá trị giữa
toFixed[]
toFixed[digits]
2 và
toFixed[]
toFixed[digits]
8, bao gồm, sẽ không gây ra
toFixed[]
toFixed[digits]
5. Việc triển khai được phép hỗ trợ các giá trị lớn hơn và nhỏ hơn như được chọn.

const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
0

Nếu phương thức này được gọi trên một đối tượng không phải là

const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
1.

Sự mô tả

toFixed[]
toFixed[digits]
0 Trả về một biểu diễn chuỗi của
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
3 không sử dụng ký hiệu theo cấp số nhân và có chính xác ____11 chữ số sau vị trí thập phân. Số được làm tròn nếu cần thiết, và phần phân số được đệm bằng số không nếu cần thiết để nó có độ dài được chỉ định. Nếu giá trị tuyệt đối của
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
3 lớn hơn hoặc bằng
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
6, phương pháp này sẽ gọi
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
7 và trả về một chuỗi theo ký hiệu theo cấp số nhân.
returns a string representation of
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
3 that does not use exponential notation and has exactly
toFixed[]
toFixed[digits]
1 digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If the absolute value of
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
3 is greater or equal to
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
6, this method calls
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
7 and returns a string in exponential notation.

Cảnh báo: Số điểm nổi không thể đại diện cho tất cả các số thập phân chính xác trong nhị phân. Điều này có thể dẫn đến kết quả bất ngờ, chẳng hạn như

const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
8 trả lại
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
9.
Floating point numbers cannot represent all decimals precisely in binary. This can lead to unexpected results, such as
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
8 returning
const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'
9 .

Ví dụ

Sử dụng tofixed

const numObj = 12345.6789;

numObj.toFixed[]       // Returns '12346': rounding, no fractional part
numObj.toFixed[1]      // Returns '12345.7': it rounds up
numObj.toFixed[6]      // Returns '12345.678900': additional zeros
[1.23e+20].toFixed[2]  // Returns '123000000000000000000.00'
[1.23e-10].toFixed[2]  // Returns '0.00'
2.34.toFixed[1]        // Returns '2.3'
2.35.toFixed[1]        // Returns '2.4': it rounds up
2.55.toFixed[1]        // Returns '2.5': it rounds down as it can't
                       // be represented exactly by a float and the
                       // closest representable float is lower
2.449999999999999999.toFixed[1] // Returns '2.5': it rounds up as it less
                       // than NUMBER.EPSILON away from 2.45 and therefore
                       // cannot be distinguished
-2.34.toFixed[1]       // Returns '-2.3': due to operator precedence,
                       // negative number literals don't return a string…
[-2.34].toFixed[1]     // Returns '-2.3'

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # sec-number.prototype.tofixed
# sec-number.prototype.tofixed

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Làm cách nào để sử dụng tofixed trong React?

Câu trả lời mã của React React..
var c = a.tofixed [1];Bảng điều khiển.log [c];/* Kết quả ->*/ 3.3 ..
var g = a.tofixed [4];Bảng điều khiển.log [g];/* Kết quả ->*/ 3.3445 ..
var g = a.tofixed [7];Bảng điều khiển.log [g];/* Kết quả ->*/ 3.3445000 ..

Làm cách nào để sử dụng tofixed trong javascript?

Phương thức tofixed [] trong javascript được sử dụng để định dạng một số bằng ký hiệu điểm cố định.Nó có thể được sử dụng để định dạng một số có số chữ số cụ thể ở bên phải số thập phân.Phương thức tofixed [] được sử dụng với một số như thể hiện trong cú pháp trên bằng cách sử dụng '.'nhà điều hành.using the '. ' operator.

Làm cách nào để làm tròn đến 2 vị trí thập phân trong JavaScript?

Sử dụng phương thức tofixed [] để làm tròn một số đến 2 số thập phân, ví dụ:const result = num.tofixed [2].Phương pháp tofixed sẽ làm tròn và định dạng số đến 2 thập phân. to round a number to 2 decimal places, e.g. const result = num. toFixed[2] . The toFixed method will round and format the number to 2 decimal places.

Làm cách nào để giới hạn số lượng vị trí thập phân trong JavaScript?

Để hạn chế các vị trí thập phân trong javascript, hãy sử dụng phương thức tofixed [] bằng cách chỉ định số lượng số thập phân.use the toFixed[] method by specifying the number of decimal places.

Bài Viết Liên Quan

Chủ Đề