Is it possible to convert a Double variable to String within a CanvasText?

1

To avoid having to declare a variable, is it possible to convert a double variable to a string within a CanvasText?

double cxg = 74.35;
canvas.drawText(cxg, 250, 10, paint); 

This gives me an error since cxg is double.

    
asked by Raúl Borgarello 15.01.2018 в 06:01
source

2 answers

0

Assuming that you are programming in Java, you can use the function String.valueOf to convert from double to String :

double cxg = 74.35;
canvas.drawText(String.valueOf(cxg), 250, 10, paint);

I hope it serves you. Greetings!

    
answered by 16.01.2018 в 07:36
0

The method drawText certainly needs a String to define the text that will be displayed:

drawText (String text, 
                float x, 
                float y, 
                Paint paint)

in this you must convert the value double to String , you can do it using these options:

Concatenating a String (empty):

canvas.drawText(""+cxg, 250, 10, paint); 

Using the method String.valueOf () :

canvas.drawText(String.valueOf(cxg), 250, 10, paint);

Using the method toString () the variable with value type double :

canvas.drawText(cxg.toString(), 250, 10, paint);
    
answered by 20.02.2018 в 18:22