This question already has answers here:
How to have sprintf to ignore trailing zeros
(2 answers)
Closed 8 years ago.
I would like to have max 3 digits after decimal point when it is necessary.Sprintf and format bank do not give me what I need. This numbers are going to be in a text box on a figure.
Basically what I tried :
tt=2.4242
sprintf('%.3f', tt)
tt=2.424
that's good for the numbers that have 3 or more digits after decimal point but what if I have no digit ( For the math guys : I mean 0 after decimal point) or 1 digit, it doesn't look that good. For example:
tt= 0
sprintf('%.3f', tt)
tt=0.000
Is there a function for that or do I have to do that with if or for?
I appreciate your help!
After sprintf('%.3f', tt), use regexprep to
remove trailing zeros, if any;
remove also the decimal point if all digits after it are zero.
That is:
regexprep(sprintf('%.3f', tt), '(\.*0+)$', '')
Examples:
>> tt = 4.1; regexprep(sprintf('%.3f', tt), '(\.*0+)$', '')
ans =
4.1
>> tt = 4; regexprep(sprintf('%.3f', tt), '(\.*0+)$', '')
ans =
4
Try g format specifier instead of f:
sprintf('%.4g', tt)
See also: How to have sprintf to ignore trailing zeros (Give #RTL is due, I asked the same question a few days ago, this is why I know the answer).
Related
This question already has an answer here:
What's the difference between double.toStringAsFixed and toStringAsPrecision in dartlang?
(1 answer)
Closed 3 years ago.
How I can display or keep double values to 2 decimal points?
textContent: 'Total Payable: ' + '€'+finalPrice.toString(),
I need to know how to ensure that
double finalPrice;
that finalPrice is always displayed to 2 decimal points. Whether it is converted to a string or not.
I've tried everything and called my MP.
double finalPrice = 0.00;
I noticed that initialising it as 0.00 seems to do something, but I need something a bit more solid.
The expected result is the client should pay for what they've purchased. The actual result is we are charging the customer a bit more than what they've purchased
double d = 1.5124;
String number = d.toStringAsFixed(2); // 1.51
Even if you have
double d = 1.51 // number: 1.51
double d = 1.5 // number: 1.50
double d = 1 // number = 1.00
You can see you will always have 2 decimal places with toStringAsFixed(2).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
As the title says i want to know if its possible to add or multiply digits of a number in matlab
eg:
for a number
123456789
add the digits, that is
1+2+3+4+5+6+7+8+9 =>45 =>4+5 => 9
advance thanks for your help
Numeric approach
A = 35356536576821;
A = abs(A);
xp = ceil(log10(A)):-1:1;
while ~isscalar(xp)
A = sum(fix(mod(A,10.^xp)./10.^[xp(2:end) 0]));
xp = ceil(log10(A)):-1:1;
end
Char approach
A = '35356536576821';
A = char(regexp(A,'\d+','match'));
while ~isscalar(A)
A = num2str(sum(A - '0'));
end
Both, first take the absolute number (strip the minus) then: the numeric one counts with log10() how many digits a number has and through modulus and divisions extracts the digits which are summed, while the char approach convert to numeric digits with implicit conversion of - '0', sums and converts back to string again.
Both approaches might suffer from floating point approximation, but the numeric one is definitely more exposed to it:
A = 11111111111111111;
xp = ceil(log10(A)):-1:1;
fix(mod(A,10.^xp)./10.^[xp(2:end) 0])
ans =
Columns 1 through 13
1 1 1 1 1 1 1 1 1 1 1 1 1
Columns 14 through 17
1 1 1 2
To have approximations with the char approach, the first sum should exceed a 16 digits number.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
convert string to number array in matlab
Is there a simple way in Matlab to convert a string like this
'123456789'
into a vector like this ?
[1 2 3 4 5 6 7 8 9]
If all you have is contiguous characters from 0 to 9:
v = double(s)-'0';
double(s) converts a string into an array where each element is the ASCII code of the corresponding character. To obtain the numberic values we subtract '0' (which is in fact 48 in ASCII) and since digits have a sequential representation in ASCII code ('1' = 49, '2' = 50, etc.) we end up with intended result.
one way would be using regexp for this. But of course it only works for single digit numbers.
>> str = '123456789';
>> num = regexp(str,'\d')
num =
1 2 3 4 5 6 7 8 9
how to display exponent value after calulation to textbox in iphone sdk.For example say 6.4516e-10.i am not getting answer for it in my textbox after calculating 10 * 6.4516e-10.please tell me solution..
Use StringWithFormat and exponent formations:
From the man page on printf:
eE The argument is printed in the style e `[-d.ddd+-dd]' where is one digit before the decimal point and the number after is equal to the precision specification for the argument; when the precision is missing, 6 digits are produced.
So you would want a format something like: %.4e
float n = 6.4516e-10;
n = n * 10;
NSLog(#"n: %.4e", n);
2011-08-29 07:36:38.158 Test[39477:707] n: 6.4516e-09
I want to convert the decimal number 27 into binary such a way that , first the digit 2 is converted and its binary value is placed in an array and then the digit 7 is converted and its binary number is placed in that array. what should I do?
thanks in advance
That's called binary-coded decimal. It's easiest to work right-to-left. Take the value modulo 10 (% operator in C/C++/ObjC) and put it in the array. Then integer-divide the value by 10 (/ operator in C/C++/ObjC). Continue until your value is zero. Then reverse the array if you need most-significant digit first.
If I understand your question correctly, you want to go from 27 to an array that looks like {0010, 0111}.
If you understand how base systems work (specifically the decimal system), this should be simple.
First, you find the remainder of your number when divided by 10. Your number 27 in this case would result with 7.
Then you integer divide your number by 10 and store it back in that variable. Your number 27 would result in 2.
How many times do you do this?
You do this until you have no more digits.
How many digits can you have?
Well, if you think about the number 100, it has 3 digits because the number needs to remember that one 10^2 exists in the number. On the other hand, 99 does not.
The answer to the previous question is 1 + floor of Log base 10 of the input number.
Log of 100 is 2, plus 1 is 3, which equals number of digits.
Log of 99 is a little less than 2, but flooring it is 1, plus 1 is 2.
In java it is like this:
int input = 27;
int number = 0;
int numDigits = Math.floor(Log(10, input)) + 1;
int[] digitArray = new int [numDigits];
for (int i = 0; i < numDigits; i++) {
number = input % 10;
digitArray[numDigits - i - 1] = number;
input = input / 10;
}
return digitArray;
Java doesn't have a Log function that is portable for any base (it has it for base e), but it is trivial to make a function for it.
double Log( double base, double value ) {
return Math.log(value)/Math.log(base);
}
Good luck.