Converting double to string
double total = 44;
String total2 = String.valueOf(total);
This will convert double to String
how convert double to string
The easiest way is to simply concatenate it with an empty String which converts it automatically:
String stringFromDouble = ss + "";
Alternatives:
String stringFromDouble = Double.toString(ss);
String stringFromDouble = String.valueOf(ss)
String stringFromDouble = new Double(ss).toString()
How to convert a double to a string with 2 decimal places?
The quick and dirty way is to use a formatted String and specify the number of decimal points. Lately there's been a trend of suggesting the usage of a DecimalFormat
instead since it will respect different locales and the usage of commas or points as a decimal separator.
//The suggested way lately
DecimalFormat formatter = new DecimalFormat("#0.00");
twoDecimals.setText(formatter.format(2.123145));
//The usual way with some caveats
twoDecimals.setText(String.format("%.2f",2.123));
I'm pretty sure it could also be done with formatted strings, but hey.. who am I to go against the trend.
Java, inside a method, cannot convert double to string
The Java class Double
contains a static method that will do what you're looking for.
Double.toString(doubleValue)
You could fix your problem in a couple of different ways, depending on the functionality you want your GuitarFubar
class to have:
public class GuitarMClass {
public static void main(String[] args) {
// OPTION 1
GuitarFubar output2 = new GuitarFubar();
output2.setGuitarLength(12.2);
String mkShft2 = output2.getGuitarLengthAsString();
System.out.println(mkShft2);
// OPTION 2
GuitarFubar bString = new GuitarFubar();
bString.setGuitarLength(12.2); //your example code never sets the value of guitarLength, which is why one of your outputs is '0.0'
System.out.println(Double.toString(bString.getGuitarLength()));
// OPTION 3
GuitarFubar guitarFubar = new GuitarFubar(12.2); // actually use the guitar length when constructing a new instance
System.out.println(Double.toString(guitarFubar.getGuitarLength()));
}
public class GuitarFubar {
private double guitarLength;
// make sure you have a no-args constructor if that's how you are going to instantiate your class
public GuitarFubar(){
}
public GuitarFubar(double guitarLength){
this.guitarLength = guitarLength;
}
// make sure you have a proper 'getter'
public double getGuitarLength(){
return this.guitarLength;
}
// make sure you have a proper 'setter'
public void setGuitarLength(double guitarLength){
this.guitarLength = guitarLength;
}
// alternative to sillyString method if you want this functionality within your GuitarFubar class
// (although not required since a caller of getGuitarLength() can handle String conversion)
public String getGuitarLengthAsString() {
return Double.toString(this.guitarLength);
}
}
}
How do I convert a double into a string in C++?
The boost (tm) way:
std::string str = boost::lexical_cast<std::string>(dbl);
The Standard C++ way:
std::ostringstream strs;
strs << dbl;
std::string str = strs.str();
Note: Don't forget #include <sstream>
how to convert double to string in android
String yourDoubleString = String.valueOf(yourDouble);
if You want to have the returned double from Your getInuNilai() Method as a String:
first get Your double from this Method:
double inuNilaiDouble = getInuNilai();
and parse it into String:
String inuNilaiString = String.valueOf(inuNilaiDouble);
or
String inuNilaiString = Double.toString(inuNilaiDouble);
if you want this outside Your DataItem.java, make a reference of DataItem and get the double:
double inuNilaiDouble = mReferenceOfDataItem.getInuNilai();
and then parse it into String like shown above.
Converting Double to String in C++
#include <iomanip>
using namespace std;
// ...
out << fixed << val;
// ...
You might also consider using setprecision
to set the number of decimal digits:
out << fixed << setprecision(2) << val;
How to convert double to String and remove all trailing zero behind the point value?
Hope this helps:
public static String format(double val) {
if(val == (long)val)
return String.format("%d", (long) val);
else
return String.format("%s", val);
}
Input:
1. 101.00000
2. 102.02000
Output:
1. 101
2. 102.02
How to convert double into string with 2 significant digits?
We can convert double to string, then check every index and take up to two nonzero (also .
) strings. But the issue comes on scientific notation for long double.
You can check Convert long double to string without scientific notation (Dart)
We need to find exact String value in this case. I'm taking help from this answer.
String convert(String number) {
String result = '';
int maxNonZeroDigit = 2;
for (int i = 0; maxNonZeroDigit > 0 && i < number.length; i++) {
result += (number[i]);
if (number[i] != '0' && number[i] != '.') {
maxNonZeroDigit -= 1;
}
}
return result;
}
String toExact(double value) {
var sign = "";
if (value < 0) {
value = -value;
sign = "-";
}
var string = value.toString();
var e = string.lastIndexOf('e');
if (e < 0) return "$sign$string";
assert(string.indexOf('.') == 1);
var offset =
int.parse(string.substring(e + (string.startsWith('-', e + 1) ? 1 : 2)));
var digits = string.substring(0, 1) + string.substring(2, e);
if (offset < 0) {
return "${sign}0.${"0" * ~offset}$digits";
}
if (offset > 0) {
if (offset >= digits.length) return sign + digits.padRight(offset + 1, "0");
return "$sign${digits.substring(0, offset + 1)}"
".${digits.substring(offset + 1)}";
}
return digits;
}
void main() {
final num1 = 0.000000000003214324;
final num2 = 0.000003415303;
final v1 = convert(toExact(num1));
final v2 = convert(toExact(num2));
print("num 1 $v1 num2 $v2");
}
Run on dartPad
Related Topics
How to Compare Two Double Values in Java
Firestore - Merging Two Queries Locally
How to Set Addsnapshotlistener and Remove in Populateviewholder in Recyclerview Item
How to Use the Legacy Apache Http Client on Android Marshmallow
Change File Owner Group Under Linux with Java.Nio.Files
In Linux, How to Execute Java Jar File with External Jar Files
Create a File Using Runtime.Exec Using Echo in Linux
The Full Pathname of a Jdk Installation for Oracle SQL Developer
Why Do I Get "Exception; Must Be Caught or Declared to Be Thrown" When I Try to Compile My Java Code
Java: How to Get Input from System.Console()
Android Jdbc Not Working: Classnotfoundexception on Driver
Recyclerview Horizontal Scrolling to Left
Android Camera Will Not Work. Startpreview Fails
How to Open a Command Terminal in Linux
Why Parallel Execution on Java Compile Take Linear Growth in Time