getNativeBalance shows 8 BNB instead of 0.8 BNB

Hello,
I have 0.8 test BNB into my wallet but getNativeBalance shows 8 BNB instead of 0.8 BNB when i run it.
Why? Where is the 0?
Thanks.

Where does it show 8 bnb? It should not return a number in bnb

I mean it shows 8 instead of 0.8 when i console log or display it(I have 0.8 into my wallet not 8, this is the problem)

And how did you get that number? That is what the api returned? Are you sure you have 0.8 bnb?

Here is the code:

const fetchNativeBalance = async () => {
    // get mainnet native balance for the current user
    const balance = await Web3Api.account.getNativeBalance({
      chain: "bsc testnet",
    });

Yes, i’m sure.

That code doesn’t print anything

I know, i didn’t add the whole code.

Here is the whole code;

import React, { useState, useEffect } from "react";
import { useMoralisWeb3Api } from "react-moralis";

function GetBalance() {
  const Web3Api = useMoralisWeb3Api();
  const [userBalance, setUserBalance] = useState("");

  const fetchNativeBalance = async () => {
 
    const balance = await Web3Api.account.getNativeBalance({
      chain: "bsc testnet",
    });


    const showBalance = balance.balance;
    setUserBalance(showBalance);
  };

  useEffect(() => {
    fetchNativeBalance();
  });

  return (
    <div>
      <h6> Your balance: {userBalance} BNB </h6>
      {console.log(userBalance)}
    </div>
  );
}

export default GetBalance;

That seems strange. What number you get if you try directly in admin interface in web3api tab?

{
  "balance": "722076000000000000"
}

It doesn’t show decimals too(on admin interface in web3Api tab)

My current BNB value= 0.7221 BNB

This is the right number, this is what you should get in your code too

I don’t have 722076000000000000 BNB :slight_smile:
How can i add decimals?

https://docs.moralis.io/moralis-server/tools/moralis-units#converting-token-value-from-wei

2 Likes

Ah thank you very much

1 Like