CodeGym /Blog Jawa /Acak /Jawa Ngonversi Dobel dadi String
John Squirrels
tingkat
San Francisco

Apa iku "dobel"?

Jinis data primitif "dobel" ing Jawa digunakake kanggo nyimpen angka desimal kanthi tingkat presisi sing luwih dhuwur nganti 15 digit. Nalika "ngambang" nyedhiyakake 6-7 digit presisi, "dobel" luwih akeh digunakake kanggo presisi lan pangukuran sing luwih dhuwur.

Apa sing diarani "Dobel" ing basa Jawa?

"Double" minangka kelas bungkus ing Jawa sing nyimpen jinis primitif "dobel". Nerangake karo akeh fungsi sing migunani kanggo manipulasi lan komputasi "pindho". Contone, sampeyan bisa ngowahi Jawa pindho kanggo string cukup efektif nggunakake obyek wrapper sawijining.

Kepiye carane ngowahi Jawa Ganda dadi String?

Sampeyan bisa ngowahi dobel dadi senar ing Jawa kanthi macem-macem cara sing gampang. Ayo katon ing sawetara conto.

Metode Double.toString().

``````package com.doubletostring.java;
public class ConvertDoubleToString {

public static void main(String[] args) {

Double pi = 3.141592653589793;
System.out.println("Pi Double: " + pi);

// Converting Double to String using Double.toString() method
String piStr = Double.toString(pi);
System.out.println("Pi String: " + piStr);

}
}``````

Output

Pi Ganda: 3.141592653589793 Pi String: 3.141592653589793
Iki minangka pendekatan sing paling praktis lan umum digunakake kanggo ngowahi basa Jawa dobel dadi senar.

Metode String.valueOf().

``````package com.doubletostring.java;
public class StringValueOfDouble {

public static void main(String[] args) {

// Converting Double to String using String.valueOf() method
}
}``````

String.format() metode

``````package com.doubletostring.java;
public class StringFormat {

public static void main(String[] args) {

Double vernierCalliper = 7.271572353580126;
System.out.println("Vernier Calliper Double: " + vernierCalliper);

// Converting Double to String using String.format() method
// Format Literal "%s" returns String for any parameter data type
// Parameter is "Double" in our case
String vernierCalliperStr = String.format("%s", vernierCalliper);
System.out.println("Vernier Calliper String: " + vernierCalliperStr);
}
}``````

Output

Vernier Calliper kaping pindho: 7.271572353580126 Vernier Calliper String: 7.271572353580126

Kesimpulan

Saiki, sampeyan kudu ngerti macem-macem cara ngowahi basa Jawa dadi senar. Nanging kanggo nguwasani apa wae, latihan minangka kunci. Good luck lan seneng coding!
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION