io:format throws a badarg exception if the format is ~f but the argument is integer:
io:format("~f", [2]).
Adding 0.0 solves the problem bus there an elegant way?
io:format("~f", [2+0.0]).
float(Number) convert Number to float
list_to_float(String) convert String to float
is_float(Term) returns true if Term is a float
If you don't care about the exact output, you can use:
io:format("~p", [Term]).
This will work with any term, but doesn't give you the same kind of formatting options as ~f would.
Either
io:format("~f", [2.0]).
or
io:format("~f", [float(2)]).
works.
Related
I have an expression in Jenkinsfile:
myvar = myvar.substring(0, myvar.length() - 2)
The goal is to cut the EoL characters from the string (which contains the execution result of the batch command).
Recently I've installed GroovyLint plugin for VSCode and it complained about this line:
Violation in class None. The String.substring(int, int) method can be replaced with the subscript operatorGroovyLint(UnnecessarySubstring-1)
I've googled what is that subscript operator and it looks like the replacement would be something like this:
myvar = myvar[0..myvar.length() - 2]
but unfortunately, it does not work: it gives no visible error, but also makes no changes to the myvar.
What do I miss? Maybe you can't use variables as part of the subscript operator?
Maybe there is a better way to cut those end-of-line characters? I guess I could use regexp, but to me, that sounds like overkill.
Thanks!
Thanks to ernset_k, found the answer!
The problem was that the subscript operator includes the upper bound. In my original scenario, last 2 characters where not printable and that's why I did not see the difference while debugging. I had to use "myvar.length()-3".
But as indicated by ernest_k, instead of calculating the length of the string you need to trim, we can also use other options of the operator. All these examples work as expected:
println myvar[0..myvar.length() - 3]
println myvar[0..<-2]
println myvar[0..-3]
I am using a function to create a list from a float.
float_to_list(0.02).
It returns:
"2.00000000000000000000e-002"
I need it to give me a number exactly like:
"0.20"
If I fed it 5.23
"5.23"
If I fed it 5.5
"5.50"
So basically the number rounded to two decimal places.
Probably an easy fix.
Thanks
EDIT:
I would like to use the io format it looks like it might work,
but it dosen't in this example:
wxTextCtrl:setValue( TcGrossProfit, io:format("~p", [NUMBER]), ),
seems textctrl wants a string, I don't want to print it to the screen.
Are you looking for something like this:
6> F = 5/2.
2.50000
7> io_lib:format("~.1f",[F]).
["2.5"]
8> io_lib:format("~.2f",[F]).
["2.50"]
9> io_lib:format("~.3f",[F]).
["2.500"]
If yes, have a look at the io_lib module.
mochinum:digits converts a float to a string with an appropriate level of precision.
1> mochinum:digits(1.1).
"1.1"
2> mochinum:digits(1.2345).
"1.2345"
Not exactly what the OP requested, but useful nonetheless.
Alternatively you could use the function you were already using.
float_to_list(0.02,[{decimals, 2}]) outputs '0.02'
Or for Elixir users ;)
:erlang.float_to_list(5.231,[{:decimals, 2}]) outputs '5.2'
This link provides functions that truncate/floor or ceil or round a float. Given those you can round to 2 digits by multiplying by 100, rounging and then dividing back by 100 (and possibly rounding again to avoid precision errors)
I know people don't like the, "I am not an expert in language X" answers, but the printf command is quite ubiquitous so I will say, look for an analog of printf in Erlang.
Edit: It looks like the format and fwrite may be those analogs. For more info from erlang.org.
Any way to convert Float to string with out getting E (exponent).
String str = String.valueOf(floatvalue);
txtbox.settext(str);
and i am using NumericTextFilter.ALLOW_DECIMAL in my textField which allow decimal but not E.
i am getting like this 1.3453E7 but i want it something like 1.34538945213 due to e i am not able to set my value in edit text.
so any way to get value with out e.
I'm not 100% sure I understand what number you're trying to format. In the US (my locale), the number 1.3453E7 is not equal to the number 1.34538945213. I thought that even in locales that used the period, or full stop (.) to group large numbers, you wouldn't have 1.34538945213. So, I'm guessing what you want here.
If you just want to show float numbers without the E, then you can use the Formatter class. It does not, however, have all the same methods on BlackBerry that you might expect on other platforms.
You can try this:
float floatValue = 1.3453E7f;
Formatter f = new Formatter();
String str = f.formatNumber(floatValue, 1);
text.setText(str);
Which will show
13453000.0
The 1 method parameter above indicates the number of decimal places to show, and can be anything from 1 to 15. It can't be zero, but if you wanted to display a number without any decimal places, I would assume you would be using an int or a long for that.
If I have misunderstood your problem, please post a little more description as to what you need.
I'll also mention this utility class that apparently can be used to do more numeric formatting on BlackBerry, although I haven't tried it myself.
Try this:
Double floatValue = 1.34538945213;
Formatter f = new Formatter();
String result = f.format("%.11f", floatValue);
Due to the floating point presentation in java, the float value 1.34538945213 has not the same representation as the double value 1.34538945213. So, if you want to get 1.34538945213 as output, you should use a double value and format it as shown in the example.
I have been pondering a multiple choice question on coercion. One of the 4 examples a,b,c or d is an example of coercion. I narrowed it down to A or B. But I am having a problem choosing between the two. Cane someone please explain why one is coercion and one isn't.
A)
string s="tomat";
char c='o';
s=s+c;
I thought A could be correct because we have two different types, character and string, being added. Meaning that c is promoted to string, hence coercion.
B)
double x=1.0;
double y=2.0;
int i=(int)(x+y);
I also thought B was the correct answer because the double (x+y) is being turned into a int to be placed in i. But I thought this could be wrong because its being done actively through use of (int) rather than passively such as "int i = x + y"
I'll list the other two options, even though I believe that neither one is the correct answer
C)
char A=0x20;
A = A << 1 | 0x01;
cout << A << endl;
D)
double x=1.0;
double y=x+1;
return 0;
I'm not just looking for an answer, but an explanation. I have read tons of things on coercion and A and B both look like the right answer. So why is one correct and the other not.
I actually think it's B. Even though there's the explicit (int), it's still type coercion (just not automatic type coercion). You're converting a floating point value (probably stored as an IEEE floating point value) to an integer value (probably stored in two's complement).
Whereas A is simply concatenating a character to a string, where a string is just a null terminated array of characters. There's no data type conversion going on there, just a bit of memory manipulation.
I could be wrong though.
EDIT: I would have to agree with Parris. Given that this is a C++ string and not a C array of characters (my mistake), the chracter in A is probably being coerced to a string.
I don't think type casting is equivalent to type coercion, which is why A would probably be the right answer.
B takes a double and casts it to an int, which is more like a conversion than a coercion. In A you aren't converting anything you're being implicit. You are telling the runtime/compiler/whatever "these 2 things are similar can you figure out how to concatenate them?"
C isn't a conversion or coercion its just bit shifting. Although the cout might be coercion... I am not sure if there is coercion to a string there to write to the console.
D might contain a coercion since 1 is an int and you are adding it to a double. However, you can do floating point math with integers having a decimal is just more explicit.
I think A is the most straight forward example of coercion. Although C's cout statement seems suspicious as well.
I would like to convert the binary string <<"abc">> into the following string "<a><b><c>" .
In other words, each byte shall be written between one "less than" char and one "greater than" char.
I suppose that the function is recursive ? Note that abc is just an example !
1>lists:flatten([[$<,C,$>]||C<-binary_to_list(<<"abc">>)]).
"<a><b><c>"
alternative
lists:flatmap(fun(C)-> [$<,C,$>] end,binary_to_list(<<"abc">>)).
or
f(C) -> [$<,C,$>].
lists:flatmap(fun f/1,binary_to_list(<<"abc">>)).
The most efficient if you want a flat list would probably be:
fr(<<C,Rest/binary>>) ->
[$<,C,$>|fr(Rest)];
fr(<<>>) -> [].
This expansion is similar to what a list/binary comprehension expands to.
Use a binary comprehension:
2> [[$<, C, $>] || <<C:1/binary>> <= <<"abc">>].
[[60,<<"a">>,62],[60,<<"b">>,62],[60,<<"c">>,62]]
So you don't have to process the binary into a list first and then work on it. It is probably a bit faster, especially for large lists, so if performance matter to you, it may be a viable alternative option.
this answer is probably not best one in terms of efficiency(i didn't compare it to other solutions) but it certainly helps to understand how you can invent your own iterators over different collections in erlang aimed for achieving your specific goal instead of using predefined iterators
fr(<<>>, Output) -> Output;
fr(<<"b", Rest/binary>>, Output) ->
fr(Rest, <<Output, "b">>);
fr(<<C:8, Rest/binary>>, Output) ->
fr(Rest, <<Output/binary, $<, C:8, $>>>).
f(Input) -> fr(Input, <<>>).
P.S. it looks like this solution is actually the most efficient :)