Comment afficher une valeur en fraction?

1 次查看(过去 30 天)
j'ai un decimal 0.3333, comment l'afficher en fraction?
Je veux avoir 1/3

回答(1 个)

Mathieu NOE
Mathieu NOE 2025-4-17
Bonjour @KINGANGA
voici une possibilité :
% Given decimal number
decimalNumber = 0.33333333333;
% Find the closest integer fraction with specified tolerance
tolerance = 1e-6;
[numerator, denominator] = rat(decimalNumber, tolerance);
% Display the result
fprintf('The closest integer fraction to %.2f with tolerance %.e is %d/%d\n', decimalNumber, tolerance, numerator, denominator);
The closest integer fraction to 0.33 with tolerance 1e-06 is 1/3

标签

Community Treasure Hunt

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

Start Hunting!