How to convert BigInteger balance from WEI to ETH in Objective-C?

I am using etherscan api to get balance using the address. But I am getting a response in wei. After some research, I got some WEI to eth converter methods.
WEI to eth
But I don't know which method will suitable for me and how I will convert this in Objective -c?

Thanks in advance.

2 Answers

ether also designates a unit (of 1e18 or one quintillion Wei) of the currency

WEI is just a simple conversion of ETH.

``````1 ETH = 10**18 WEI = 1000000000000000000 WEI
1 WEI = 1/10**18 ETH = 1/1000000000000000000 ETH
``````

So in the end, all you have to do is divide the value by 1**18.

• So am I correct with this : `float a = [[dictWalletData valueForKey:@"result"]floatValue] / 1000000000000000000;` Feb 13 '18 at 10:02
• Theoretically yes. Be aware though, that 10**18 is a number that does not fit into a float. You need to implement some method to store numbers that big or use a library. Alternatively you could process it completely with strings, if you do not need to do any math with the result afterwards. Feb 13 '18 at 10:06
• I thought the max value for a float in Objective-C is 340282346638528859811704183484516925440. Much bigger than 10**18. Feb 13 '18 at 10:13
• You are right. Excuse my mistake. You will be able to handle the numbers in Objective-C Feb 13 '18 at 10:16
``````var balance=await web3.eth.getBalance(web3.utils.toChecksumAddress(address));
response.balance=web3.utils.fromWei(balance);
``````

Web3 provides a utility to deal with all these bigNumber conversions. So never risk playing around

• Thanks for answering! The OP asked for code in Objective-C, though, and this appears to be web3.js (JavaScript). If you are trying to argue that Objective-C should not be used, please state your case clearly (especially bearing in mind that there is an answer explaining how to do what the OP wants in Objective-C.) Cheers! Sep 25 '20 at 11:03