Search code examples
androidtextviewandroid-edittextbitcoindivide

Kotlin Convert Bitcoin value into Satoshi Value


//-----------------------------------------------------------------------------------

Hi, this is my very first "ask". I am happy to be a part of the community. I want to obtain the value of a Satoshi coin (it's 100.000.000 of 1 BTC) I am able to find the value of 1 BTC in USD$. Basically, can I just divide the value of 1BTC in USD$ with 100.000.000? But how can I put the value into a TextView or EditText?

//-----------------------------------------------------------------------------------

Debug Error.

E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.thordalenterprise.satoshiconverter, PID: 19493
java.lang.NumberFormatException: For input string: "45,586"
    at java.lang.Integer.parseInt(Integer.java:615)
    at java.lang.Integer.parseInt(Integer.java:650)
    at com.thordalenterprise.satoshiconverter.MainActivity$loadPrice$1.onResponse(MainActivity.kt:65)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.processTask(ThreadPoolExecutor.java:1187)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:929)W/atoshiconverte: Accessing hidden method Landroid/media/AudioManager;->getOutputLatency(I)I (greylist, reflection, allowed)V/AudioManager: isWiredHeadsetOn...I/Process: Sending signal. PID: 19493 SIG: 9

//-----------------------------------------------------------------------------------

// Functions
private fun loadPrice() {
    val request: Request = Request.Builder().url(BPI_ENDPOINT).build()
    val EurView:TextView = findViewById<TextView>(R.id.EURreturn)
    val usdView:TextView = findViewById<TextView>(R.id.USDRETURN)
    val btcView:TextView = findViewById<TextView>(R.id.BitcoinViewReturner)
    val SatoshiC:EditText = findViewById(R.id.SatoshiCount)
    val ddx:EditText = findViewById(R.id.editTextNumber)

okHttpClient.newCall(request).enqueue(object : Callback {
    override fun onFailure(call: Call?, e: IOException?) { }
    override fun onResponse(call: Call?, response: Response?) {
        val json = response?.body()?.string()

        val read = Scanner(System.`in`)

        // we get the json response returned by the Coin Desk API
        // make this call on a browser for example to watch the properties
        // here we get USD and EUR rates properties
        // we split the value got just to keep the integer part of the values
        val btcRate:String = "1"
        val SatoshiCoin:Int = 100000000

        val usdRate = (JSONObject(json).getJSONObject("bpi").getJSONObject("USD")["rate"] as String).split(".")[0]
        val satosh = usdRate.toInt() / 100000000

        val num = 2.toBigDecimal().divide(2.toBigDecimal()).toInt()

        //Problem start
        val satoshiValue = usdRate.toInt() / SatoshiC.text.toString().toInt() * satosh

        btcView.text = satoshiValue.toChar().toString()
        //Problem end

Solution

  • java.lang.NumberFormatException: For input string: "45,586"

    means your string contains , you should remove , format the string properly.

    example 45,586 instead you should pass 45586 as string