How can I convert function result? [duplicate] - erlang

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to format a number with padding in Erlang
I developed this function :
check() ->
case get_val(Text, H) of
{ok, {Montant}} -> io:format("~s", [Montant]);
{error, pas_echeance} -> io:format("erreur")
end.
this function displayed Montant, this is an example of Montant :45
My goal is to convert this value "45" (which denotes Tunisian dinars) in the "45000" form (so I would have Tunisian millime -- each dinar consist of 1000 millimes).

Why dont you convert the value to integer (using list_to_integer("45") or list_to_integer(get_val(Text,H)) and multiply it by 1000.

Related

Round the number [duplicate]

This question already has answers here:
ultimate short custom number formatting - K, M, B, T, etc., Q, D, Googol
(3 answers)
Closed 6 months ago.
Hello How I can round that number without using custom formats like this "[>999999]0.0,,\M;[>999]0.0,\K;0"
Because if y use the custom format with this formula '="EXAMPLE "&E24' puts the value without that format.
try:
="example "&IF(A1<1000, A1,
IF(A1<1000000, TEXT(A1/1000, "#.0")&"K",
IF(A1<1000000000, TEXT(A1/1000000, "#.0")&"M",
IF(A1<1000000000000, TEXT(A1/1000000000, "#.0")&"B",
TEXT(A1/1000000000000, "#.0")&"T"))))

Delphi - inconsistent floating point roundoff when using FormatFloat('0.##',argValue) [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Trying to round off double values to two decimal places using function FormatFloat (Format string '0.##').
Below are the input and output values
231.545 -> 231.54 (but expected output is 231.55)
2.315 -> 2.31 (but expected output is 2.32)
23.045 -> 23.05 (gives expected output 23.05)
23.145 -> 23.14 (but expected output 23.15)
23.245 -> 23.25 (gives expected output 23.25)
23.345 -> 23.34 (but expected output 23.35)
23.445 -> 23.45 (gives expected output 23.45)
23.545 -> 23.55 (gives expected output 23.55)
23.645 -> 23.65 (but expected output 23.64)
23.745 -> 23.75 (gives expected output 23.75)
23.845 -> 23.84 (but expected output 23.84)
23.945 -> 23.95 (gives expected output 23.95)
why this strange behaviour happening? am using Delphi 7.
Binary floating point values can not represent every value exactly. This is what you are seeing.
For example, the value 2.315 is represented in double precision by:
2.31499 99999 99999 94670 92948 17992 48605 96656 79931 64062 5
This will be rounded to 2.31
If you can use a decimal data type, like currency, you can get the wanted output (if currency is within limits of your working range):
var
c : Currency;
begin
c := 2.315;
WriteLn(FormatFloat('0.##',c)); // Outputs 2.32
end.
An alternative is to use a decimals library, like BigDecimals, but that would require a modern Delphi version with support of records with methods.

Why does use of := with fmt.Scanf in Go always return 1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here is the code:
var n int
a, _ := fmt.Scanf("%d",&n)
Then a == 1, n has changed its value by input. Why does use of := with fmt.Scanf in Go always return 1?
fmt.Scanf() returns the number of successfully scanned items:
Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.
So if your input is a valid integer number fitting into an int, fmt.Scanf() will succeed to parse it and store it in n, and so it will return 1.
Should you input an invalid number (e.g. the string value "a"), scanning would not succeed, so 0 would be returned along with a non-nil error, like in this example:
var n int
a, err := fmt.Sscanf("a", "%d", &n)
fmt.Println(a, err)
Which outputs (try it on the Go Playground):
0 expected integer

Lua Text and function print in the same line [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Can any body help me ?
function sum(_g, _h)
local num = (_g * _h) / 2
return num
end
print("The result is")(sum(10, 6))
Why this isn't working ?
The function print takes one or more strings as arguments.
When the strings are entered as different arguments, it outputs them separated by a tab
The result is 20
To get this output, just imagine you store the return from sum in a variable
res = sum(10, 6)
And then call print entering your string and the result just like you enter 10 and 6 in your function sum:
print("The result is ", res)
This also traduces to
print("The result is ", sum(10, 6))
Without needing to store the result anywhere.
Anyway if you target an output which looks like
The result is 20
you must enter only one string as the argument for print
..
is the operator which lets you concatenate two string in one string, so that "hello".." world" results in "hello world".
Now just combine the two strings "The result is " and 20 (which actually is a number, but it gets automatically converted to a string) with the .. operator, as in
res = sum(10, 6)
mystring = "The result is "
print(mystring..res)
Or, more shortly
print("The result is "..sum(10, 6))

Creating byte buffers in rust [duplicate]

This question already has answers here:
Creating a vector of zeros for a specific size
(4 answers)
Closed 7 years ago.
What is the most efficient way to get access to &mut [u8]? Right now I'm borrowing from a Vec but it would be easier to just allocate the buffer more directly.
The best I can do right now is to preallocate a vector then push out its length but there is no way this is idiomatic.
let mut points_buf : Vec<u8> = Vec::with_capacity(points.len() * point::POINT_SIZE);
for _ in (0..points_buf.capacity()) {
points_buf.push(0);
}
file.read(&mut points_buf[..]).unwrap();
You could just create the vec with a given size directly:
vec![0; num_points]
Or use iterators:
repeat(0).take(num_points).collect::<Vec<_>>()

Resources