How to print double value in DecimalFormat - decimalformat

I have a code.
Double value = 6.589715E7;
DecimalFormatSymbols symbol = new DecimalFormatSymbols(Locale.GERMANY);
symbol.setGroupingSeparator(',');
final DecimalFormat doris = new DecimalFormat("#,###000",symbol );
System.out.println(changedValue);
which displays the value like this rite now.
65,897150
is there any way out this can be displayed as.
65,9 (also rounding off to one decimal place)

This example rounds up 2 but you get the point, try this
inputValue = Math.Round(inputValue, 2);

Here I put answer in string format. you can also convert into double format.
Double value = 6.589715E7;
DecimalFormatSymbols symbol = new DecimalFormatSymbols(Locale.GERMANY);
symbol.setGroupingSeparator(',');
final DecimalFormat doris = new DecimalFormat("##,###000",symbol );
String str = String.valueOf(doris.format(value));
String formated=str.replace(".", ",").substring(0,str.indexOf(",")+2);
Log.e("Log","d result="+formated);

Related

How decode binary string react native

how could I convert a string in binary to string again in react native?
Ex: 01010 to Hello
I have the code to convert string to binary
Ex: text.split('').map(l => l.charCodeAt(0).toString(2)).join(' '),
let txt="Hello".split('').map(l => l.charCodeAt(0).toString(2)).join(' ')
let s = txt.split(" ").map(w=> String.fromCharCode(parseInt(w,2)))
console.log(s.join(""))
Just convert binary string back to integer and map those values to character using fromCharCode
To convert binary string to sting you can use parseInt and String.fromCharCode
parseInt converts strings to number. First argument is string value and the second argument is the base value in which the string representation is.
let num = parseInt(binaryStr, 2);
String.fromCharCode converts character code to matching string.
let str = String.fromCharCode(65);
Complete code with example:
const text = 'Hello'
let binaryStr = text.split('').map(l => l.charCodeAt(0).toString(2)).join(' ');
console.log('Binary string:', binaryStr);
//To convert binary to string
let strVal = binaryStr.split(' ').map(l => String.fromCharCode(parseInt(l, 2))).join('');
console.log('String:', strVal);
Output of above code:
Binary string: 1001000 1100101 1101100 1101100 1101111
String: Hello

How can a decimal number with a comma be converted in a decimal (Point) in SwiftUI?

How can a textField were a comma is inserted be Formated so the inserted Decimal value example 10,05$ can be converted to a decimal(Point) 10.05$?
private static let Intrest = "Intrest"
private static let Repayment = "Repayment"
var loan: Double {
let intrest = Double (Intrest)
let repayment = Double (Repayment)
let Loan = Intrest + Repayment
return Loan
}
var body: some View {
VStack{
HStack{
Text ("Repayment %")
Spacer()
TextField("2 %", text: $Repayment)
.keyboardType(.decimalPad)
}
VStack{
HStack{
Text ("Loan / month")
}
TextField("2 %", text: $Loan)
.keyboardType(.decimalPad)
}
}
The problem only occurs because in many countries mostly comma is used which breaks the calculation.
Thanks
You need to make sure you convert your string into a double with point and not comma.
This one should help.
Swift playground - How to convert a string with comma to a string with decimal
I suggest you read Textfield not updating and use custom string binding with double back storage.
Specifically read the Update there :-)
Complete example usage of custom string binding with backend double number is here https://gist.github.com/op183/24607b6309f7d1ea8f3aaf02d17e4ed3

How to generate a random double value in a specific range using Kotlin?

How do I generate a random number in a specific float range (From 51.3257 to 52.4557 for example) using Kotlin?
var xCoord = randomValue()
var yCoord = randomValue()
Do I need to make a method or do I just import something?
Here is the documentation for that: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/-random/index.html
I believe that it would be something like
import kotlin.random.Random
var xCoord = Random.nextDouble(51.3257, 52.4557)
var xCoord = Random.nextDouble(51.3257, 52.4557)
try this
double random = ThreadLocalRandom.current().nextDouble(51.3257, 52.4557);
If you are using kotlin do this :
var max=51.3257
var min=52.4557
var outpot= (min + Random.nextDouble() * (max - min))
Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
Please try this. Thanks

String interpolation outputs enum name instead of value

I was hoping someone can explain this default functionality regarding string interpolation and the enum type.
I have this enum:
public enum CommentType
{
MyComment = 24,
TheirComment = 25,
AnotherComment = 26
}
I am using it in a string:
Dim sDateModified As String
sDateModified = $"<div name='commenttype{CommentType.MyComment}'></div>"
I was expecting CommentType.MyComment to be evaluated and the int value 24 to be used. The result should be: <div name='commenttype24'></div>
But what actually happens is that the identifier is used instead, giving me: <div name='commenttypeMyComment'></div>
In order to get the enum value I had to convert it to an integer:
sDateModified = $"<div name='commenttype{Convert.ToInt32(CommentType.MyComment)}'></div>"
It just feels counter intuitive to me. Can someone explain or point me to documentation on why it works this way?
You're getting the string value MyComment because that's what is returned by:
CommentType.MyComment.ToString()
Methods like String.Format and Console.WriteLine will automatically call ToString() on anything that isn't already a string. The string interpolation syntax $"" is just syntactic sugar for String.Format, which is why string interpolation also behaves this way.
Your workaround is correct. For slightly more compact code, you could do:
CInt(CommentType.MyComment)
You just need to use a format string in the interpolated value to force the result to integer format.
Dim sDateModified As String
sDateModified = $"<div name='commenttype{CommentType.MyComment:D}'></div>"

VB 2010: How to index textbox (making it like slots)?

If the title isn't clear; I want to be able to select any character from textbox without making some complex loops-dependent code (I can do that one). For example, let's consider this text is entered in a textbox:
hello user!
I want some syntax when I tell to get me the index 1's value, it gives me "h", for index 5 = "o"... etc
So, anyone knows what's the right syntax, please help!
string can be directly indexed without any special code.
//from metadata
public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>
{
....
// Summary:
// Gets the character at a specified character position in the current System.String
// object.
//
// Parameters:
// index:
// A character position in the current string.
//
// Returns:
// A Unicode character.
//
// Exceptions:
// System.IndexOutOfRangeException:
// index is greater than or equal to the length of this object or less than
// zero.
public char this[int index] { get; }
....
}
dim str = "hello";
dim hchar = str(0);
dim echar = str(1);
dim lchar = str(2);
ect
Dim x As String = "Hello"
Console.Write(x.IndexOf("e")) 'Would return the position
Console.Write(x(x.IndexOf("e"))) 'Would return the character based upon the position
Console.Write(x(1)) 'Returns the character based at position 1 of the string
You can remove the Console.Write if you are using a WinForm.
TextBox1.Text = x(x.IndexOf("e"))
This should work.
Dim orig = "hello user!"
Dim res = Enumerable.Range(0,orig.Length).[Select](Function(i) orig.Substring(i,1))
So then you can do:
Dim x = res(0) 'x = "h"