Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to replace all occurrences of a specific digit in a floating-point number with Kotlin

1 Answer

0 votes
fun replaceAllDigits(num: Double, oldDigit: Char, newDigit: Char): Double {
    // Format the number to a string with 3 decimal places
    val strNum = String.format("%.3f", num)

    // Replace all occurrences of oldDigit with newDigit
    val modifiedStr = strNum.map { if (it == oldDigit) newDigit else it }.joinToString("")

    // Convert the modified string back to Double
    return modifiedStr.toDoubleOrNull() ?: 0.0
}

fun main() {
    println("%.3f".format(replaceAllDigits(82420.291, '2', '6')))
    println("%.3f".format(replaceAllDigits(111.11, '1', '5')))
}



/*
run:

86460.691
555.550

*/

 



answered Nov 18, 2025 by avibootz

Related questions

...