setting decimal point precision in handles.object

5 visualizaciones (últimos 30 días)
Barak
Barak el 18 de Nov. de 2022
Editada: Fangjun Jiang el 18 de Nov. de 2022
I got some GUIDE GUI thingy that has a editable text field that I send some calculation to it base on user input in another 3 fields (also ediable text boxes.
How do I set that the display will be of 2 point after the decimal point? the standard routine of ('somethng %f.2'. variable) does not work in that case of:
function calcBtn_Callback(hObject, eventdata, handles)
SLa = str2double(get(handles.inputFld1,'String'));
SLb = str2double(get(handles.inputFld2,'String'));
SLc = str2double(get(handles.inputFld3,'String'));
semiPrim = abs((SLa + SLb + SLc) / 2);
area = sqrt(abs(semiPrim*(semiPrim-SLa)*(semiPrim-SLb)*(semiPrim-SLc)));
set(handles.triAreaCalcFld,'String', area); % <-- HOW TO MAKE this a 2 places after decimal precision?

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 18 de Nov. de 2022
Editada: Fangjun Jiang el 18 de Nov. de 2022
As you can see, the value for "editable text field" should be string.
You can't do "set(handles.triAreaCalcFld,'String', area)" as "area" is numerical.
You shall do
AreaString=sprintf('%.2f',area);
set(handles.triAreaCalcFld,'String', AreaString)
  1 comentario
Fangjun Jiang
Fangjun Jiang el 18 de Nov. de 2022
actually, the opposite of str2double(), num2str(), can do that doo.
num2str(pi,'%.2f')
ans = '3.14'

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by