简体   繁体   English

从 api 提取更好的格式数据

[英]better format data pulled from api

asked a question a couple days ago about formatting data pulled from an api and i got some fantastic answers, though: im getting the following error on pages that the api doesnt have data for certain assets:几天前问了一个关于格式化从 api 提取的数据的问题,但我得到了一些很棒的答案:我在 api 没有某些资产数据的页面上收到以下错误:

在此处输入图像描述

for example (with the code highlighted above removed):例如(删除上面突出显示的代码):

在此处输入图像描述

    const columns = [{
      title: 'Rank',
      ...
    }, {
      title: 'Symbol',
      ...
    }, {
      title: 'Name',
      ...
    }, {
      title: 'Price',
      dataIndex: 'Price',
      key: 'current_price',
      render: (value) => {
        return <span>$<b>{value.toFixed(2)}</b></span>;
      },
    }, {
      title: 'Market Cap',
      ...
      },
    }, {
      title: 'Change (24hr)',
      dataIndex: 'priceChange',
      key: 'price_change_percentage_24h',
      render: (value) => {
        return <span><b>{value.toFixed(2)}</b>%</span>;
      },
    },
    }];

TL;DR im having trouble trying to give me the results that i currently have and not give me an error when null. TL; DR 我在尝试给出我目前拥有的结果时遇到了麻烦,并且在 null 时没有给我一个错误。 BIG BONUS POINTS for anyone who can help me also only show 2 numerals after the decimal point IF greater than 1.0 and 3 or 4 numerals after the decimal point if less than 1.0 for prices (as shown below)对于任何可以帮助我的人,如果价格大于 1.0,小数点后仅显示 2 个数字,如果价格小于 1.0,小数点后仅显示 3 或 4 个数字(如下所示)

在此处输入图像描述

i apologize for the noobish question but i can't seem to come up with a solution and figured id learn best if someone could kindly assist me我为这个愚蠢的问题道歉,但我似乎无法想出一个解决方案,如果有人能帮助我,我认为我学得最好

Use isNaN() to check if it's not a number and show empty string then check for greater than 1 and use 2 decimals else use 3 (or 4 if you like)使用isNaN()检查它是否不是数字并显示空字符串,然后检查大于 1 并使用 2 个小数,否则使用 3(或 4,如果你喜欢)

return (
  <span>
    <b>
      {isNaN(parseFloat(value)) ? '' : value > 1 ? value.toFixed(2) : value.toFixed(3)}
    </b>
    %
  </span>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM