Used code below to save value for float
domainInstance.standardScore = params["standardScore"] as float
In this case my input was given as 17.9 and in db2 database saving as 17.899999618530273 but I want to save as 17.9 itself, let me know how to do it
You can't set precision to a Float or Double in Java. You need to use BigDecimal.
domainInstance.standardScore = new BigDecimal(params["standardScore"]).setScale(1, BigDecimal.ROUND_HALF_UP);
The method BigDecimal.setScale(1, ...) limits decimal to one place only. The second parameter is the rounding strategy.
You need to use BigDecimal to do Conversion from String, then BigDecimal(value).floatValue() to get float, You can do this on more that one way, examples
1 - Using setScale in BigDecimal
def temp = new BigDecimal(params["standardScore"]).setScale(1, BigDecimal.ROUND_HALF_UP)
2- Using DecimalFormat
DecimalFormat df = new DecimalFormat("#.0");
def temp = new BigDecimal(df.format(params["standardScore"] ))
Then you need to get the float value
domainInstance.standardScore = temp.floatValue()
Related
I am trying to use loop to write float into a file, but the write function does not let it happen until I convert it into string or use format, which eventually converts it into string.
Is there any way that I can do it? I need the data inside the file to be in float, since later on the file data can be used to create graphs, thus strings can not be a way out.
Later on I need to use Termgraph Python3 library for this, and the data needs to be a float.
print("sequence","Sign","Values")
f = open("termdata.dat","w")
f.write("Sequence,Score\n")
for i in range(0,len(list_one)):
value1 = list_two[i]
value_ = q_score[value1]
print(list_one[i],"\t", list_two[i],"\t", value_)
str1 = str(list_one[i])
float1 = float(value_)
f.write(str1)
f.write(",")
f.write(str(float1))
f.write("\n")
I'm adding lines to a memo box from a sql query dataset. The data is being added using the memo1.lines.add function looping through the dataset
memo1.Clear;
frmMain.Query2.Open;
try
while not frmMain.Query2.Eof do
begin
memo1.Lines.Add(frmMain.Query2.FieldByName('mass').AsString);
frmMain.Query2.Next;
end;
finally
frmMain.Query2.Close;
end;
Everething from this point is working fine, the only problem is that because the data is being converted into string format is loses the trailing 0 .ie
Float value = 40.50 String Value = 40.5 .
Float value = 42.40 String Value = 42.4 .
Float value = 40.53 String Value = 40.53 .
Is there any way this could be rectified? FormatFloat doesn't want to format it because it's not a float anymore. StrtoInt doesn't work because of the 3 sting values in the memobox.
Instead of using AsString retrieve your values as Float values and the use FloatToStrF. E.g.
memo1.Lines.Add(FloatToStrF(frmMain.Query2.FieldByName('mass').AsFloat, ffNumber, 15, 2));
I like Format.
Format('%.2n', [frmMain.Query2.FieldByName('mass').AsFloat]);
I am slowly understanding things in swift, I am coming for a javascript background so it is somewhat familiar.
However variables are urking me.
in JS a variable can be
var varName = 1; //Number
var varName2 = "petey" //String
var conCat = varname + varName2; // 1petey
however in swift String vars and In var are troubling me. All I want to do is capture decimal number from user input from multiple "textField" sources and store that data to variable (which i already have setup) I need to concatinate a few of them with + then do basic math with the data in the variables with * and /.
How do I make the textFeld only capture?int or how do I convert text field strings to numbers, e.g. int?
A UITextField contains a String, not an Int, so you have to convert it, e.g.
let number : Int? = textField.text.toInt()
So, there actually is a method to convert from String to Int built-in in Swift. Beware, that it returns an optional, because the conversion may fail. So you have to check for nil before you use the variable.
For your other question, take a look at e.g. UITextField - Allow only numbers and punctuation input/keypad. Basically, you will have to adapt UITextField and filter it, once there are new entries. On iOS it might be easier, because you can show a numbers-only keyboard.
The data send from the textField is String. There are multiple ways to convert an Int to a String.
var a:String="\(2+3)" //"5"
And to concatenate a String to Int :
var b:String="Hello "+"\(3*4)" //"Hello 12"
And to Convert a String to an Int from a textField:
var b:Int=textField.text.toInt()
Use the function in swift inputmethod.intValue(); if any text is entered then it returns with a 0.
When doing format string interpolation in Sweden I get a comma instead of a dot when creating strings with decimal numbers:
scala> val a = 5.010
a: Double = 5.01
scala> val a = 5.0101
a: Double = 5.0101
scala> f"$a%.2f"
res0: String = 5,01
My question is, how do I set the format so that I get the result 5.01? I would like to be able to set the locale only for that String, i.e. so that I don't change the locale for the whole environment.
Cheers,
Johan
Using the same Java library number formatting support accessible
from StringOps enriched String class, you could specify another locale just for that output:
"%.2f".formatLocal(java.util.Locale.US, a)
(as described in "How to convert an Int to a String of a given length with leading zeros to align?")
The Scala way would be to use the string f interpolator (Scala 2.10+), as in the OP's question, but it is using the "current locale", without offering an easy way to set that locale to a different one just for one call.
Locale.setDefault(Locale.US)
println(f"$a%.2f")
Create a method called parseEqn which will receive 1 String variable and return the double value of the expression passed to it.
parseEqn("123+23") → 146.0
parseEqn("3+5") → 8.0
parseEqn("3-5") → -2.0
so thats the question^^^^ and i think what i need to do is first use a string tokenizer to split the string up and then convert the tokens into doubles and from there add or subtract depending on the operator...but im not sure..
this is what i have so far
public double parseEqn(String str) {
StringTokenizer st = new StringTokenizer(str, "+-", true);
String first= st.nextToken();
String op= st.nextToken();
String second= st.nextToken();
double num1 = Double.parseDouble(first);
double num2 = Double.parseDouble(second);
if (op.equals("+")){
return num1+num2;
}
else (op.equals("-")){
return num1-num2;
}
i have no clue though....
Writing an expression parser is not a trivial task. The standard algorithm for parsing arbitrary infix expressions is the shunting-yard algorithm. The idea is to run through each token and build a Reverse Polish Notation (RPN) expression from the input. An RPN expression is essentially a stack-based list of operations that is very easy for a computer to work with (and easy to write code to evaluate).