how to remove dot from string in lua - lua

I would like remove dot from string.
For example
242.701000393 = 242701000393
I have tried below code which is working fine in some cases.
string.gsub("242.701000393", "%.", "")
Same way, I have tried above function for 100999212.707000393 .But it's not working for it.
Am newbie in lua.I would like to just remove .(dot) from string in every cases.
Share your thoughts as i don't know how to achieve it.
As per my logic looks like below
split a string by dot and convert into an array
concat all array elements
Share it solution if possible.
Thanks in advance.
CODE :
local destination_number =100999212.707000393
destination_number = string.gsub(destination_number, "%.", "")
print(destination_number)
Output : 100999212707
Expected output : 100999212707000393

The issue is the accuracy of the number - floating point rounding, not the function of gsub.
local destination_number =100999212.707000393
print(destination_number, type(destination_number) )
destination_number = string.gsub(destination_number, "%.", "")
print(destination_number,type(destination_number))
output
100999212.707 number
100999212707 string
compared to ...
local destination_number = "100999212.707000393"
print(destination_number, type(destination_number) )
destination_number = string.gsub(destination_number, "%.", "")
print(destination_number,type(destination_number))
output
100999212.707000393 string
100999212707000393 string
A floating point double has about 15 digits of accuracy, which means the 393 is being lost in the generation of the number. When converted to a string, it has already gone.
15 digits is quite accurate, and normally good enough for most purposes, but if it is insufficient for you, you will need to consider alternative data representations.

Related

Generate Random String with Specific Restrictions

Tring to generate a random string but it needs to be formatted a specific way.
N = number
L = Capital Letter
must be NL-NN
needs hyphen as well
examples: 5K-22, 9L-19, 0R-66
every method I have tried has just generated a string but without the hyphen, I know it is probably something simple my brain just hurts thinking on it so I thought I'd see if one of yall could give me a hand.
Thanks
Try this:
function randomchar(a,b)
return string.char(math.random(string.byte(a),string.byte(b)))
end
a=randomchar('0','9')
b=randomchar('A','Z')
c=randomchar('0','9')
d=randomchar('0','9')
print(a..b..'-'..c..d)

Converting type string to long

I am trying to convert the type of string to long in the following code:
PaymentReceived = String.Format(new CultureInfo("en-IN", true), "{0:n}", t.PaymentReceived),
Here t.PaymentReceived is of type long, and the PaymentReceived is of type string but I want it to be of type long.
I am using this to convert the PaymentReceived value into comma separated value.
I am trying to do as of my knowledge like
PaymentReceived = Convert.ToInt64( String.Format(new CultureInfo("en-IN", true), "{0:n}", t.PaymentReceived))
But the error is Additional information: Input string was not in a correct format.
So please help me with another solution, thank you.
The formatter n, adds additional non-numeric characters. For en-IN culture, that means a number like 1000 ends up as 1,000.00.
The Convert.ToInt64 method requires that the string be 100% numeric, including no period, which might be fine for Convert.ToDecimal, but a long is not a float. Therefore, emphatically, your string is not formatted correctly, and the error is both obvious and correct. I'm not sure what your ultimate goal here is, but it makes no sense to convert a long to a formatted string and then immediately convert it back to a long, anyways.
Assuming you have only the string and you need to format it as a long, then you need to ensure that it's formatted as a long should be. That requires:
Split on the decimal point and take just the left side:
str = str.Split(new[] { '.' })[0];
Replace any commas with empty strings:
str = str.Replace(",", "");
That assumes you know the format will something like 1,000.00. Otherwise, you may want to use a regex to replace all non-numeric characters with an empty string, instead. However, you still need to split on the decimal. Otherwise, if you just removed all non-numeric characters from something like 1,000.00, then you'd end up with 100000, a number 100 times larger than the actual string number. Also, this is all dependent on the culture. Some cultures use , as the decimal separator and . and delimiter in large numbers. If you need to handle various cultures, you'll need to adjust accordingly.

Double operations with FR or UK typed double

Description
If you want to store FR typed double in a node, you can only do it using String value, because double values are displayed like this: 12,58 (instead of 12.58 for US type).
So, if you want to do some computing in a cypher query, it's hard to use FR types doubles since they are String, and String + String results in a concatenation, not a real addition.
Example
To check this problem, you can simply create two nodes:
CREATE (a:TestNode{value : "12,45"}), (b:TestNode{value : "15"})
RETURN (a.value + b.value) AS result
Solutions ?
The obvious solution would be to store everything as double in the base, and do the computing on Java side, but it requires too much refactoring, and the problem is that some values are stored as FR double (like 1254,7898), some are stored as UK double (like 12,789.45) and some as US double (those ones are stored using the primitive type double).
I'm looking for a pure Cypher solution, to avoid massive data and code refactoring.
Interesting question. You can handle the differently formatted doubles in a CASE expression. First, you convert it to a string. If it contains both . and , it's UK. Else if it contains , but not . it's FR. Else it should be a native double. You can then replace the , and convert to float.
MATCH (a:TestNode)
RETURN
CASE
// UK formatted, 12,3456.45
WHEN toString(a.value) =~ ".*\\..*" AND toString(a.value) =~ ".*\\,.*"
// remove , and convert to float
THEN toFloat(replace(toString(a.value), ',', ''))
// FR formatted 1234,23
WHEN toString(a.value) =~ ".*\\,.*" AND NOT toString(a.value) =~ ".*\\..*"
// replace, with . and convert to float
THEN toFloat(replace(toString(a.value), ',', '.'))
// else it should be a double
ELSE toFloat(a.value)
END
But this query can only RETURN the value as a float/double. I don't know how you can include the CASE thing in a more complex query.

How to convert Float to String with out getting E in blackberry

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.

Error in string.gsub with backslash

local a = "te\st"
local b = string.gsub(a,'\','\\\\')
assert(false,b)
What am I doing wrong?
When I do assert, I want that to the screen the string te\st will be printed... but it's not working
I have a JSON file, that I want to decode it into Lua table. I don't need to print out nothing, I did the assert just to test a local problem.
So what I need is to keep all data in the JSON file that has '\'.
Use [[]] instead of "" or '' if you don't want backslash to have special meaning.
Read about literal strings in the manual.
Have you tried escaping it with the % character instead of \
I don't know if this will help, but I was having a HELL of a time making Lua's gsub match my string with special characters in it that I wanted treated literally... it turned out that instead of using \ as an escape character, or doubling the character, that I needed to prefix the special character with % to make it be treated literally.
Your question wasn't too clear so I'm not 100% sure what you mean. Do you mean that you want the assert to fire when b is equal to the string "te\st"? If so you can do a simple:
assert(b ~= "te\st")
Or I suppse...
assert(b ~= a)
You don't need the gsub. But here it is anyways.
local a = "te\\st"
local b = string.gsub(a,'\\','\\')
assert(false,b)

Resources